I am working an a Hashing based program .
My question is will the HashCode of a String will remain the same for the entire application .
The reason i was asking this because , the KetamaMemcachedSessionLocator inside Mecached Servers works this way
If there are two servers on which Memcache is running , i want to locate a key from a Particular server .
String key = "MyString";
int keyid = key.hashCode();
int v = keyid % 1; //( I assume that this will contact the First Server to retrieve that value )
int v = keyid % 2; //( I assume that this will contact the Second Server to retrieve that value )
String value = MemcachedClient.get(key, v);
Followed to implement the above based on this website
http://dev.mysql.com/doc/refman/5.0/en/ha-memcached-using-hashtypes.html
please share your views , incase if you find any issues if the above way it works .
According to hashcode contract it will always the same if
string1.eqauls(string2)The java.lang.String hash functionIn an attempt to provide a fast implementation, early versions of the Java String class provided a hashCode() implementation that considered at most 16 characters picked from the string. For some common data this worked very poorly, delivering unacceptably clustered results and consequently slow hashtable performance.
From Java 1.2, java.lang.String class implements its hashCode() using a product sum algorithm over the entire text of the string. Given an instance s of the java.lang.String class, for example, would have a hash code h(s) defined by
where terms are summed using Java 32-bit int addition, s[i] denotes the ith character of the string, and n is the length of s.
As with any general hashing function, collisions are possible. For example, the strings “FB” and “Ea” have the same hash value. The hashCode() implementation of String uses the prime number 31 and the difference between ‘a’ and ‘B’ is just 31, so the calculation is 70 × 31 + 66 = 69 × 31 + 97.
Check
Collections Framework Enhancements in Java SE 7as you see there are changes in it and who knows will be.The alternative hash function is only applied to keys of type String.