• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 537
  • Last Modified:

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

what is the purpose of hash code and equals method  in hashset
Manikandan Thiagarajan
Manikandan Thiagarajan
1 Solution
zzynxSoftware engineerCommented:
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
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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now