Question

Java doc for method String#hashCode() says:

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

Questions:

  • Is it possible to have same hash code for two string objects having different values? If yes then please share some examples.
  • Is it possible to get String value back from its hash code?

I am not using it any where in code. I have just asked this question to know more about Java String class.

Was it helpful?

Solution

Is it possible to have same hash code for two string objects having different values? If yes then please share some examples.

Here is a small sample of randomly generated examples of short strings with identical hash codes:

String 1   String 2   Common hash code
--------   --------   ----------------
VTBHKIGV - FLXCLLII        -1242944431
FPESRBAH - GNFWMYVA         1778061647
UYDHRTXL - HGCNRCBE         1509241566
VXQMFMDE - YMYXDWKK        -1553987354
VGWBSYRX - JZNQSUXK          700334696

Since multiple strings can share the same hash code, restoring the original from the hash is not possible.

OTHER TIPS

Is it possible to have same hash code for two string objects having different values?

yes, how can you map infinite string possibilities to int without it

Is it possible to get String value back from its hash code?

no, read 1

It's absolutely possible to have two different strings (or objects) with the same hash code. That's why we have collision handling. So in general it's not possible to get the string value back from the hash code. This is because the hash code value quickly overflows the 32-bit integer for strings longer than 4 bytes.

assume your string is 2 characters long

c1,c2

your hash is 31*c1 + c2

can you think of different values that will map to the same hash?

it is worse in longer strings

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top