hashCode()
:
If you only override the hash-code method nothing happens, because it always returns a new hashCode
for each object as an Object class.
equals()
:
If you only override the equals method, if a.equals(b)
is true it means the hashCode
of a and b must be the same but that does not happen since you did not override the hashCode
method.
Note : hashCode()
method of Object class always returns a new hashCode
for each object.
So when you need to use your object in the hashing based collection, you must override both equals()
and hashCode()
.