• 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

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
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

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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
reply
    Bookmark Topic Watch Topic
  • New Topic