Search code examples
javastringuniquehashcode

is the int value of String.hashCode() unique?


I encountered a problem days ago.Now i have tens of millions of words,type of string. now i decide to keep them in database and use index to keep them unique.And i do not want to compare the original words to keep them unique. I would like to make sure whether the hashCode() method of a string can be unique , will it not be changed if a use another laptop or different time or something like that?


Solution

  • Below is the hashCode computation of a String which a JVM does. As stated it purely calculates based on the individual character and its position in the String and there is nothing which is dependent on JVM or the machine type which runs the JVM which would alter the hashcode.

    This is also one of the reason why String class is declared final (not extensible leading to immutability) so that no one alters its behaviour.

    Below is as per spec:-

    public int hashCode()
    

    Returns a hash code for this string. The hash code for a String object is computed as

    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
    

    using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)