• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

hashMap duplicate key

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..i was expecting output should be x y....but its y y ...how can key overlap??...

thanks..

regards Akshay
 
Sheriff
Posts: 27465
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your equals() method may be incorrect because you've used the == operator to compare two String objects. The == operator only returns true when the two String objects are the same object; you may want to compare the two String objects to see if they contain the same sequence of characters, in which case you should use equals() to do that.

However in your example you have two A objects, both of which have name = "y". You set those values in lines 43 and 47. So after that, in line 49, when you print the name values of those two A objects you should expect to see "y" and "y", regardless of all of the obfuscation supplied by that map.
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akshay, you are changing the key AFTER you put it in the Map (a.name = "y";). The Map cannot guarantee uniqueness in such cases. This is why it's recommended to use immutable keys...
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic