two unequal objects with same hashcode
2) It is not required that if two objects are unequal according to the equal(), then calling the hashcode method on each of the two objects must produce distinct values.
Depending on the hashing function, 2 different objects can have the same hash code. However, 2 objects which are the same must produce the same result when hashed (unless someone implemented a hashing function with random numbers in which case it's useless)
For example, if I am hashing integers and my hashing function is simply (n % 10)
then the number 17
and the number 27
will produce the same result. This does not mean that those numbers are the same.
Example with Strings (all the strings below have a hashcode of 0):
public static void main(String[] args) {
List<String> list = Arrays.asList("pollinating sandboxes",
"amusement & hemophilias",
"schoolworks = perversive",
"electrolysissweeteners.net",
"constitutionalunstableness.net",
"grinnerslaphappier.org",
"BLEACHINGFEMININELY.NET",
"WWW.BUMRACEGOERS.ORG",
"WWW.RACCOONPRUDENTIALS.NET",
"Microcomputers: the unredeemed lollipop...",
"Incentively, my dear, I don't tessellate a derangement.",
"A person who never yodelled an apology, never preened vocalizing transsexuals.");
for (String s : list) {
System.out.println(s.hashCode());
}
}
(stolen from this post).