Under what conditions can two different objects have the same hashcode () ..?
What I know: -
" int hashCode()
returns the memory address of the object as the default hash value for the object.
If references x
and y
denote two different objects, the expression is
(x.hashCode() == y.hashCode())
not always false
So, I want to ask in what cases the hash values ββof two different objects are the same.
source to share
You can override hashCode
in your classes. You usually override it along with the override equals
, so if a.equals(b)
true, then it also matters a.hashCode() == b.hashCode()
(even if (a == b)
false).
However, even if it a.equals(b)
is false, it a.hashCode() == b.hashCode()
may still be true.
As you can see in the Javadoc of the Object class:
- 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.
- The two objects are not required to be unequal according to the java.lang.Object.equals (java.lang.Object) method and then calling the hashCode method on each of the two objects should have integer results . However, the programmer should be aware that getting separate integer results for unequal objects can improve the performance of hash tables.
source to share
HashCode does not always return a memory address (which itself can be bogus as objects can be moved into memory). A class that provides its own hashCode
might have an algorithm that causes collisions (two different objects with the same hash code).
In addition, you can use equals
: two objects, where a!=b
but a.equals(b)
is true, must have the same hashcode or specific data structures such as hash maps, hash codes, LRU caches, etc., not will work as expected. However, if two objects that are not equal share the same hash code, this is not a problem. hashCode
used in many cases as a performance hint (eg in hashMap). While poor hashCode implementations such as return 1;
will not cause a properly written data structure to fail, it will result in poor performance (for example, in the case of HashMap, amortized O (1) becomes O (N)).
Third, even the best hash code will necessarily have collisions if there are more than 4,294,967,296 different objects of this class. This is because there are only 4,294,967,296 hashcode values ββother than the hashcode, because hashCode is an int and pigeonhole.
Contract for Object#hashCode()
and all overriding implementations:
If two objects are equal according to the equals (Object) method, then calling the hashCode method on each of the two objects should produce the same integer result.
It is not required that if two objects are unequal according to the java.lang.Object.equals (java.lang.Object) method, then calling the hashCode method on each of the two objects should 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.
source to share