wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HashMap vs TreeMap data retrival. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap vs TreeMap data retrival." Watch "HashMap vs TreeMap data retrival." New topic
Author

HashMap vs TreeMap data retrival.

manoj sam
Greenhorn

Joined: Apr 06, 2007
Posts: 4
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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."


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap vs TreeMap data retrival.
 
Similar Threads
Sort a Map on the <Key>.
hashmap key values
suggetion needed
Map: no duplicates allowed?
Collections