This is the hashCode() from the openjdk String class:
Is this implementation thread-safe?? With the exception that many threads could compute the hash (and even all of them) but none of them could get a erroneous value from the h = hash and hash = h assinghments right??
Also, does the hashCode() must return only positive integers??
Finally, what if there is an overflow in the int arithmetic there? How to handle this case in our hashCode() implementations ??
Code:
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
Also, does the hashCode() must return only positive integers??
Finally, what if there is an overflow in the int arithmetic there? How to handle this case in our hashCode() implementations ??