Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals and Hashcode

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

If equals method is overridden, we must override the hashcode method right? is it true the other way round too?

Thanks.
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you override either equals() or hashcode(), then you have to override the other one too.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Vidhya.
 
Manju Kavi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its not mandatory to override if you do override one.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No its not mandatory to override if you do override one.


well, if we don't override we will not get any compile time error or run time exception, but it won't work as desired.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a contract between Equals and Hashcode method.

If 2 objects are equal according to Equals method, then their Hashcodes should be same.

As long as this contract is fulfilled, the program will work as required.
 
vipin jain
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Arjun,

really concept is

if equal() true -then Hashcode () must true => must overriding
if equal() false -then Hashcode() not dependen=> not necessary
if Hashcode() false- Then equal() must false=> must overriding
if Hashcode() true-> Then only request to equal() true=> might be overriding .

it base of my besat knowledege.
if any one no more about this, welcome to him/her.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic