• Post Reply Bookmark Topic Watch Topic
  • New Topic

When we use treeMap and input duplicate Key... the overided value is displayed... why is it notin hm  RSS feed

 
Ranch Hand
Posts: 1078
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we use treeMap and input duplicate Key. the overided value is displayed whenver we tried to print the value using get(key)..why is this not the same thing in HashMap
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean?

If you call put on a TreeMap or HashMap and the key already exists, then the existing value will be replaced. There's no difference with regard to this between TreeMap and HashMap - both will do the same.

Can you show some code to explain more clearly what your question is about?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:When we use treeMap and input duplicate Key. the overided value is displayed whenver we tried to print the value using get(key)..why is this not the same thing in HashMap


As Jesper states, that's false. For all Maps, if you put() with a key that's already present, the old value is replaced with the new one.

If you're not observing this behavior in both Maps, then it's almost certainly one or more of the following:

  • You didn't properly override equals() in your key class.
  • You didn't propertly override hashCode() in your key class.
  • Your compare() or compareTo() method for your key class has a bug.
  • Your compare() or copareTo() method for your key class is not consistent with its equals().
  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!