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.

+3


source to share


3 answers


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.
+6


source


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.

+1


source


The implementation of "return memory address" from hashCode

is the default. Almost always, when you use an object type in a HashMap or HashSet or whatever, you override hashCode

your own implementation, which may have nothing to do with the memory address of the object.

0


source







All Articles