Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java's hashCode() deterministic? [duplicate]

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)?

like image 331
etzourid Avatar asked May 08 '13 15:05

etzourid


People also ask

Is hashCode deterministic?

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.

Is hashCode always unique in Java?

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.

Can two unequal objects have same hashCode in Java?

Unequal objects must have different hash codes – WRONG! Objects with the same hash code must be equal – WRONG!

Is hashCode of string unique?

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.


1 Answers

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:

Formula from Wikpedia

(image taken from Wikipedia)

In some cases there is not even a sensible deterministic definition for the hash code.

like image 156
Stefano Sanfilippo Avatar answered Oct 21 '22 17:10

Stefano Sanfilippo