hashing an object means "finding a good, descriptive value (number) that can be reproduced by the very same instance again and again". Because hash codes from Java's Object.hashCode()
are of type int
, you can only have 2^32
different values. That's why you will have so-called "collisions" depending on the hashing algorithm, when two distinct Objects produce the same hashCode.
Typically, this does not produce any problems, because hashCode()
is mostly used together with equals()
. For instance, a HashMap
will call hashCode()
upon its keys, to know whether the keys may already be contained in the HashMap. If the HashMap does not find the hash code, it's obvious the key is not contained in the HashMap yet. But if it does, it will have to double-check all keys having that same hash code using equals()
.
I.e.
A.hashCode() == B.hashCode() // does not necessarily mean
A.equals(B)
But
A.equals(B) // means
A.hashCode() == B.hashCode()
If equals()
and hashCode()
are implemented correctly.
For a more precise description of the general hashCode
contract, see the Javadoc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…