Re: String and hash value
Bruintje Beer wrote:
Is a hashvalue for a string using the function String.hashCode() always
unique.
If by "unique" you mean that any distinct String has a distinct hashCode(), it
is not guaranteed.
Have you read the Javadocs for hashCode()? The API docs are very often a
perfect first place to find such answers.
<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode()>
It is /not/ required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
(emph. orig.)
You can examine the particular algorithm for String at
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#hashCode()>
Even without Javadocs it is easy to prove that the hash codes for Strings
cannot be unique for all possible String values. There are only 2**32
possible hashCode() results. Any String longer than two characters has a
larger value set than that. The sum of the value set counts for Strings of
any length is larger still.
Gotta love those Javadocs.
--
Lew