What's a good hash code if it doesn't identify a unique object?
Java API - Class Object
Hash code:
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 different integer results. However, the programmer should be aware that getting separate integer results for unequal objects can improve the performance of hash tables.
What consistency is achieved through hashing if two objects can produce different integer results? It seems strange that two different objects can return the same hash value.
source to share
There are only 2 32 integral values ββthat a hashcode can have (at least in Java). If you have more possible objects, then two different objects having the same hash value are inevitable. We do our best to avoid these "hash collisions" , but often this is simply impossible mathematically (see the principle of pigment holes ).
We usually try to design hash functions so that their outputs are evenly distributed within their range, thereby making conflicts rare.
source to share
What's a good hash code if it doesn't identify a unique object?
The hash code does not allow you to identify a unique object, but it does allow you to identify the unique group in which the object is located.
Then you only need to look at this group to find your object.
For example, a HashMap might have 500 items divided into 1000 groups. Thanks to the hash code, he immediately knows which of these 1000 groups to look to, rejecting 999 others.
If this group has 0, 1, or even 6 items, it still represents a big time saving compared to looking at all 500.
source to share