Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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

 
Ranch Hand
Posts: 1087
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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().
  •  
    You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
    Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
    htttp://www.e-iceblue.com/free-apis.html
      Bookmark Topic Watch Topic
    • New Topic