aspose file tools*
The moose likes Beginning Java and the fly likes how to Implement equals and hashcode method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to Implement equals and hashcode method" Watch "how to Implement equals and hashcode method" New topic
Author

how to Implement equals and hashcode method

Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108
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.


Thanks and Regards, Pradeep Kumar
SCJP 1.6, SCWCD 5.0
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

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.


http://muhammadkhojaye.blogspot.com/
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
Thanks Muhammad. I too was curious about the answer to this one. Your approach sounds good to me.


Rajeev Trikha (SCJP 6)
Pradeep Kumar
Ranch Hand

Joined: Sep 08, 2006
Posts: 108

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

Joined: Apr 12, 2009
Posts: 449

You can recalculate the stored hash code logic in the setter method of the mutable fields.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to Implement equals and hashcode method