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
hashCodemust 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
equalsmethod, then their hashes should be equal too.
- If two or more objects are not equal according to the
equalsmethod, then their hashes can be equal or unequal.
Remember: If you override the
equalsmethod, it is crucial to override the hash method as well.
The following code shows how to declare a hashCode: