Witscale Test Center

14.6 Overriding the equals() and hashCode() methods > 14.6.5 The contract of hashCode() method


14.6.5 The contract of hashCode() method

The HashCode() method should be consistent

If you invoke hashCode() on the same object more than once during an execution of a Java program, the it must consistently return  the same integer, provided no information used in hash code calculation and equivalence comparisons on the object is modified.

The HashCode() for equal objects must return identical integer value.

If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.

The HashCode() for unequal objects need not return identical integer value.

It is not mandatory that if two objects are unequal according to the equals() method, then calling the hashCode() method on each of the two objects should produce distinct integers. However, generating distinct hash codes for unequal objects may improve the performance of collection that use hashing.