• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap values are overriden when the key has the same hashcode  RSS feed

 
nelom singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.


 
Paul Clapham
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56536
172
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 22827
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!