GeeCON Prague 2014*
The moose likes Java in General and the fly likes is it Necessary to override hashCode whenever I override equals()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "is it Necessary to override hashCode whenever I override equals()?" Watch "is it Necessary to override hashCode whenever I override equals()?" New topic
Author

is it Necessary to override hashCode whenever I override equals()?

Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1273

Is it compulsory to override hashCode() every time I override equals(). if so, why it is so? I can alone override equals to check object equality.


No Kaustubh No Fun, Know Kaustubh Know Fun..
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1273

now i do understand it
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14195
    
  20

There's a relation between the equals() and the hashCode() methods for a class. The rules are briefly as follows: When two objects referred to by variables 'a' and 'b' are to be considered equal, then a.equals(b) == b.equals(a) == true, and a.hashCode() == b.hashCode().

If you write the equals() and hashCode() methods in such a way that this does not hold, then you will get strange bugs if you put those objects in a HashSet or if you use them as keys in a HashMap.

It's always a good idea, if you override equals(), to also override hashCode() in such a way that the above rule is satisfied.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

hashCode() is used in cases where the object is being used as a key in the map. And the equals() is used to check for equality of objects. If the objects are equal then their hashCode has to be equal and if their hashCode is equal it doesn't necessarily mean the equals() should be true.

Please SearchFirst to get a lot of replies on similar queries.


Mohamed Sanaulla | My Blog
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1273

Mohamed Sanaulla wrote:hashCode() is used in cases where the object is being used as a key in the map. .


What about HashSet and LinkedHashSet?? Don't we require to override mention methods when use this.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14195
    
  20

Yes, ofcourse the same holds for HashSet, LinkedHashSet, LinkedHashMap and any other hash-based collection class.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4398
    
    8

An important point is that you never know, at the time you write it, how your class is going to be used in the future (if you're writing anything that isn't trivial). So it's irrelevant whether you are planning to use it in a HashMap or a HashSet or any other specific collection type. It might get used that way in the future. And whoever is using it in the future (you or anyone else) is entitled to assume that you've implemented it correctly. So that means yes, you do need to override hashCode() when you override equals(). If you don't then you are deliberately deciding to break the contract as specified for the equals() method.
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1273

Thanks Guys, for your valuable reply.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Override hashCode if you want to use your objetcs in hash-based collection.

Do best to make overriden hashCode() return different int value.

Do not change values used in hashCode and equals when storing objects in hash-based collections.

To know more about hash-based collections please cover my indepth tutorials


True person is moral, false is right!
 
Consider Paul's rocket mass heater.
 
subject: is it Necessary to override hashCode whenever I override equals()?