File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap vs TreeMap data retrival.

 
manoj sam
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the best data structure to use to retrieve data from a map of data HashMap or TreeMap? There are about 40000 objects in a Map with index of Integer values.

I heard that even though TreeMap is sorted, it has nothing to do with data
retrival perfomance.

any idea please.


thanks,
manoja.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is ofcourse not one data structure that is better than the other one for all purposes. It depends on what you are doing. If you want the data to be sorted on keys, use TreeMap. Otherwise use HashMap.

HashMap is faster than TreeMap for get and put operations. From the JavaDoc of HashMap:

"This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets."

From the JavaDoc of TreeMap:

"This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations."
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic