aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes override hashCode() and equals() to store an object inside hashMap not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "override hashCode() and equals() to store an object inside hashMap not working properly" Watch "override hashCode() and equals() to store an object inside hashMap not working properly" New topic
Author

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

Eslam Farag
Greenhorn

Joined: Aug 01, 2012
Posts: 1
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

Joined: Sep 26, 2006
Posts: 9390
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1507
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
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 ..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: override hashCode() and equals() to store an object inside hashMap not working properly