Hi,
For the
Java String's hashcode() implementation:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
(jdk source code):
int h = 0;
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
I am curious to find out the reason for using base 31.
From a performance perspective, choosing a binary number such as 32 would make the 0(n) multiplications faster (left bit shift).
I am guessing 31 has been proven to give the best hashcode distribution on random string values?
Thanks
Victor