• 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

how to Implement equals and hashcode method

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

I create a POJO and override the equals and the hashcode method correctly. I also implement the Comparable and implement the methods correctly.

Now i can be safe that I can use the instance of this class in a HashSet or as a key in the HashMap.

If i add this and then modify the state of the object using the setter method its hashcode will not be the same and hence the i cannot get the value using this instance as a key from the hashMap. Also the contains method of Set will return false.

How to avoid this. One solution that i know is to make it immutable or mention in the javadoc that the instances of this class is not a suitable candidate for hash based collections. But the POJO is not specifically designed for a particular collection.

Any other view on this.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If i add this and then modify the state of the object using the setter method its hashcode will not be the same and hence the i cannot get the value using this instance as a key from the hashMap. Also the contains method of Set will return false.
How to avoid this. One solution that i know is to make it immutable


Hashcode is best when calculated from immutable data;

If you need mutable fields included in the hashCode method then you can calculate and store the hash value when the object is created and whenever you update mutable field, you must first remove it from the collection(set/map) and then add it back to the collection after updating it.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Muhammad. I too was curious about the answer to this one. Your approach sounds good to me.
 
Pradeep Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok. one possible solution. Then of course we need to mention in the javadoc of the POJO that the hashcode is caculated using the mutable fields and hence clients using the instance of this POJO should remove it from the hash based collection and then add it once the object state is modified. But there is no other go to let the client code know other than from the java doc right?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can recalculate the stored hash code logic in the setter method of the mutable fields.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recalculating a hash code from inside set methods? That sounds like a sure-fire recipe for confusion. You can calculate the hash code when it is required with the hashCode method. You can consider caching a hash code in an immutable object, or use lazy evaluation.
Look for the chapter in Bloch's book or Google for Angelika Langer Java hash code equals.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic