• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does override equals need override hashCode?  RSS feed

 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just recently I was told on this forum that if you override the equals method of an object that you should override the hashCode of the object as well.
Why is that? The object class seems to do a decent job handling the hashCode. Why not just continue to use the hashCode which is provided by Java/Oracle?
What'd more, you actually don't need to override the hashCode method, as you class and the program can/will still work fine.

From what I can see this can be a fairly simple method to override and there are libraries out there that help you do this. That is not the question that I'm asking here.

I did do some quick research and I didn't really fine anything via Google as to why you should do this, but I did find some people stating that you should.

Edit: typo in the question
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is the contract of the hashCode() method which states that equal objects must have equal hash codes.

Consider this example
It prints:
true
2125039532 312714112


Objects referenced by a1 and a2 are equal but their hash codes are different.
This can cause unexpected behaviour and strange bugs when using a code that assumes that the contract is fulfilled.
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much @Paweł Baczyński that clears things up for me.
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a Small example program :

Output:


When we use the default hashcode, we see a different output vs when we use our own hash code.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:Just recently I was told on this forum that if you override the equals method of an object that you should override the hashCode of the object as well.
...
I did do some quick research and I didn't really fine anything via Google as to why you should do this, but I did find some people stating that you should.

You don't understand technical part what guys trying to demonstrate with examples, or you don't understand in general why is that needed? If latter, then you need to read how hash table works to understand what is all about.
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda,

Sorry, Paweł Baczyński provide a nice little example and explanation shortly after my initial post, which I've thanked them for. 
Now I have a fairly good idea as to why one would need this. To does seem rather straight forward to me now.

Thanks for your time with this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!