• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashCodes

 
Sudhanshu Mishra
Ranch Hand
Posts: 236
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
here is a question regarding the hashCodes().Is it necessary to override hashcode() after overriding equals() or do we have to do so only for hashMap-using collections?
Thanks....
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you should. If you don't you are breaking the contract for the hashCode() and equals() methods as specific in the java.lang.Object class. HashMaps and HashSets make use of that contract, but other code might as well. There's no way you can guarantee that your class will never be used in a way that makes use of the contract, so you should follow it. Especially since it's not particularly difficult (in fact, modern IDEs will do it for you!).

By not doing, you are in effect "lying" about the specification of your class, as far as other developers are concerned.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic