I was reading the paragraph quoted below from an article entitled- Java theory and practice: Hashing it out – Defining hashCode() and equals() effectively and correctly
Defining equality
The Object class has two methods for making inferences about an object’s identity: equals() and hashCode(). In
general, if you override one of these methods, you must override both,
as there are important relationships between them that must be
maintained. In particular, if two objects are equal according to the
equals() method, they must have the same hashCode() value (although
the reverse is not generally true).[emphasis added by me]
My question relates to the latter bit of the paragraph “although the reverse is not generally true”. How is it possible for two different instances of a class to have the same hashCode but not be equal?
In simple terms hashcode () is a function to generate hash by some formula, so there can be some collisions, two different values can turn out to have same hashcode.
If I simply calculate the hashcode by taking mod by 6, then two different values might be having same hashcode.