File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now i do understand it
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, ofcourse the same holds for HashSet, LinkedHashSet, LinkedHashMap and any other hash-based collection class.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1283
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys, for your valuable reply.
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic