The hashCode method is an inbuilt method that returns the integer hashed value of the input value.
Here are a few key concepts to remember:
- Multiple invocations of the
hashCode
must return the same integer value within the execution of a program unless the Object used within the equals method changes. The integer value need not be the same for the next implementation. - If two or more objects are equal according to the
equals
method, then their hashes should be equal too. - If two or more objects are not equal according to the
equals
method, then their hashes can be equal or unequal.
Remember: If you override the
equals
method, it is crucial to override the hash method as well.
Code
The following code shows how to declare a hashCode: