Possible Duplicate:
why do String.hashCode() in java is not implemented in a way with less conflicts?
For non-cryptographic hashes, how does Java's String.hashCode()
perform?
Mostly I'm concerned about collisions.
Thanks.
Possible Duplicate:
why do String.hashCode() in java is not implemented in a way with less conflicts?
For non-cryptographic hashes, how does Java's String.hashCode()
perform?
Mostly I'm concerned about collisions.
Thanks.
You seem to be misunderstanding what .hashCode()
is for with regards to Java, and more specifically, the .equals()
/.hashCode()
contract specified by java.lang.Object
.
The only part of the contract of matter to anyone is that if two objects are equal with regards to .equals()
, then they must have the same hash code as returned by .hashCode()
. There is no other obligation to that contract.
It is therefore perfectly legal to write a custom .hashCode()
implementation like this, even though this is as suboptimal as one can think of:
@Override
public int hashCode()
{
// Legal, but useless
return 42;
}
Of course, JDK developers would never be that thick, and .hashCode()
implementations for builtin types (including String
) are good enough that you do not even need to worry about collisions. Even then, this implementation will more than likely vary from one JDK implementation to another, and so will its "cryptographic value".
But that's not the point.
The most important thing to consider is that .hashCode()
has nothing to do with cryptography at all. Its only obligation is to obey the contract defined by java.lang.Object
.
It's pretty good as a general purpose hash function. i.e. you shouldn't usually worry about it.
In particular:
int
range.Obviously, it is not a cryptographic hash function, so don't use it for that. Also, be aware that you likely will get hash collisions as it is producing a 32-bit hash. So you just need to design your algorithms to take that into account.