We need to override hashCode() method in every class that overrides equals() method because if we do not do this it may result in a violation of the above mention general contract between hashCode and equals

The general contract of hashCode and equals is:

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

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.

It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

Output

true

true

The above program return true because we override the equals method based on name and id i.e. two site objects which have same name and id are considered to be equal. Now let us think about set and map. Set does not contains a duplicate element and map does not contains duplicate key. Let us try to implement the set and map with above example. We override the toString method to clearly understand the implementation.

Output

false

false

The above program return false because we override the hashCode method only. For comparing two objects, equals method only compares the references. Now let us think about set and map. Set does not contains a duplicate element and map does not contains duplicate key. Let us try to implement the set and map with above example. We override the toString method to clearly understand the implementation.

Let us understand the put method of map with hashCode. For first object hashCode returns 1 based on which index value is calculated. It returns 1 in our case so at index 1, first object will be inserted which contains four elements hash, key, value and next. When second object is inserted into map, hashCode again returns 1 and equals method comes into existence to compare key value. Ideally as two objects looks identical so first object should be replaced by second but as equals method is not implemented and by default it only compare references, hence two objects are treated as different that’s why second object will be inserted into linked list. As location for both objects are at index 1 so next of first object will contains the address of second object.

In the result, we can clearly see that in both set and map there are duplicate elements and duplicate keys respectively. It violets the set and map implementations.

Above problems can be resolved by overriding both hashCode and equals method.