Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

override hashCode() and equals() to store an object inside hashMap not working properly

 
Eslam Farag
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i overrided hashCode() and equals() in a class (Dog) in order to store and retrieve it's instances from a hashMap, the code is as follows:



the problem is that, at 2 i changed the name of the dog object that's stored inside the hashMap at 1, the expected output at 3 is NULL but the actual is Dog Key!! i expect it to fail in the equals() method as clover!=arthur but it succeds!! i noticed that when the hashCode succeds (i.e. the lengh==6) the value stored in the map is retrieved even though the equals() method fails, i changed == and used equals() instead but no changes happens, the problem remains.
 
Joe Harry
Ranch Hand
Posts: 10087
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you expect it to print NULL at line 3? You are setting the name d1 in the Dog class at Line 2 but you are not overwriting that in your Map. The Map object value that points to d1 still has Dog Key!
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Eslam Farag,

Welcome to CodeRanch!

The problem is with your equals methodthat you are modifying the key after putting it in HashMap.

Hint: What does happen when you check object equality with '=='?

On a side-note, your code is a good example of why we need immutable class. As per good programming practice, key of a HashMap should be an immutable class' object (or at least nobody should be able to mess with it after its put in Map)

I hope this helps.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know it is wrong but i thought that(taking above example) when we do m.get(d1)), internally hashcode method is called on d1 object(dog object), which means it would return hashcode as 6. now it will go and look in bucket with say a marker 6 on it. inside the bucket it will use the equals method implementation , which in above case will return false(whether we use == or equals method of String class) and hence the output should be NULL and NOT "Dog Key".
 
Henry Wong
author
Marshal
Pie
Posts: 20894
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eslam Farag wrote:i overrided hashCode() and equals() in a class (Dog) in order to store and retrieve it's instances from a hashMap, the code is as follows:



the problem is that, at 2 i changed the name of the dog object that's stored inside the hashMap at 1, the expected output at 3 is NULL but the actual is Dog Key!! i expect it to fail in the equals() method as clover!=arthur but it succeds!! i noticed that when the hashCode succeds (i.e. the lengh==6) the value stored in the map is retrieved even though the equals() method fails, i changed == and used equals() instead but no changes happens, the problem remains.



First, Anayonkar is absolutely correct. You have violated the contract of the map -- keys have to be immutable. If they are not, then it is not guarranteed to work correctly !! Do *not* change key values after they have been placed in the map..... but ..... you got lucky, as an implementation detail, it looks like your violation didn't break the map.

So, second, Joe is correct. The map does *not* make a copy of keys or values instances. So, if you add an instance to the map, and then change the value of the instance, the map points to the same instance which includes the new values. So, your map points to the key, that used to be "clover", that you changed to "arthur".

Henry
 
ayush raj
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was about to post this question from K&B . I have a clear concept regarding HashCode and equals method but i am not able to understand the hashMap , how does the mapping of key-value pair takes place , how is the 'bucket' chosen and what is the content of the 'bucket' and on what criteria the object/value is retrieved . Thanks in advance ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic