• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the Difference Between Tree Map or Hash Map  RSS feed

 
Anthony Alexander
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one tell what is the difference between a tree map and a hash map?

Is there any noticable pros and cons between the two?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They do basically the same thing -- you call the same methods to use them. But there are two main differences: HashMap is faster, but noticeably so only for big collections (thousands of items or more); and if you ask a TreeMap to list all its keys for you (i.e., you call ketSet().iterator()) you'll get all the keys in sorted order. This means that the keys have to implement the Comparable interface, or you have to provide a Comparator when you create the TreeMap. On the other hand, HashMap requires that the keys overridden the hashMap() and equals() methods in a sensible way.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
On the other hand, HashMap requires that the keys overridden the hashMap() and equals() methods in a sensible way.


hashcode() and equals(), that is.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or hashCode() as the case may be.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!