• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMap values are overriden when the key has the same hashcode

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I learnt that in case of HashMap if the two keys have the same hashcode they land in the same bucket and that the Entry object is stored in a LinkedList.

However , when i ran the following code i found that if i insert two different objects with same hashcode in the map they latter overrides the previous value.


I am also attaching the code segment . Please help me understand why i cannot see two different entry objects with key having the same hashcode.


 
Marshal
Posts: 28193
95
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
Look at what you are using for keys. You said

i found that if i insert two different objects with same hashcode in the map they latter overrides the previous value



But you aren't using two different objects for keys. You're only using one object all the time, namely the Integer whose value is 1. Try actually using the Animals and Vegetables objects as keys, which I believe is what you meant to try.
 
nelom singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply i corrected the code as


and got the output as :
For Key com.example.Maps.Vegetables@1 Value is Carrot
For Key com.example.Maps.Vegetables@1 Value is Cabbage

so both keys with same hashcode are stored in my hashmap in the same bucket.

Thanks
 
Marshal
Posts: 79178
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That equals method is incorrect. It must never throw an Exception, but you will suffer a ClassCastException if you pass an object of any other type.
Don't write
if (boo) return true; else return false.
It is simply
return boo;
Try
return obj instanceof Vegetable && ((Vegetable)obj).name.equals(this.name);
That will still cause problems if you manage to set the name to null. Find out about the Objects class.

Your hashCode method might fulfil the “letter of the law” of its general contract but it is a very bad idea to return the same value from all instances. Again find out about the Objects class.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Your hashCode method might fulfil the “letter of the law” of its general contract but it is a very bad idea to return the same value from all instances.



Correct, but isn't the OP doing this in order to test the case when the hashcode are the same? ie. it is the test class to see what happens with different object that have the same hashcode.

Henry
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if the idea is to test when the hash codes are the same, you can write a “wrong” method. I hadn't noticed that bit. Sorry.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That equals method is incorrect. It must never throw an Exception, but you will suffer a ClassCastException if you pass an object of any other type.



For example if you uncommented line 53 in the last posted code; that would result in the map trying to compare the new Animals object with the Vegetables objects which are already in the Map. Perhaps that was why the OP left that line commented.
 
reply
    Bookmark Topic Watch Topic
  • New Topic