is java's hashCode() deterministic?
I try to implement a document search engine that uses the minhashing algorithm and I use hashCode to pre-hash words. Is the same word going to get the same hash every time that I run it?
Is it going to get the same hash even if I run it from a different machine (32 bit vs 64bit)?
Hash functions are deterministic. Hence, when we pass the same input to the hash function, it always generates the same output hash code, e.g. SHA-1 hashes are 160 bits long.
The hashcode is always the same if the object doesn't change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithms like hashtable, hashmap etc. An object can also be searched with this unique code.
Unequal objects must have different hash codes – WRONG! Objects with the same hash code must be equal – WRONG!
If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code. The hash code itself is not guaranteed to be stable.
It depends on the class you are referring to. Base Object.hashCode
implementation is not, since, as stated in the documentation:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
Addresses are not deterministic, consider that sometimes they are even used as a source of entropy.
But, for instance, String
has a deterministic hash code determined as follows:
(image taken from Wikipedia)
In some cases there is not even a sensible deterministic definition for the hash code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With