what is the purpose of hash code and equals method in hashset

what is the purpose of hash code and equals method  in hashset
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
 
krakatoaCommented:
hashCode() is a (hopefully) non-colliding 32 bit integer affording the object it refers to a unique handle. equals() is the way that handle gets compared to another object's hashCode, to determine if it is the same object or not. The same for HashSet, where it's used to determine if the object is itself a set - of the same size and with the same membership.
0
 
zzynxSoftware engineerCommented:
0
 
Sharon SethCommented:
To that matter , hashCode() and equals() method in any Object , is a way of  specifying what it means for two objects to be equal .
Say you have two integers 2 and 3 . You simply say they are not equal . But when it comes to Objects , say an Employee object , you have to very clearly put down when you can say two employee objects are equal (or not equal) . hashCode() and equals() are the methods that put down this logic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.