I am reading up the code of the HashMap class provided by the java 1.6 API and unable to fully understand the need of the following operation (found in the body of put and get methods) :
int hash = hash(key.hashCode());
where the method hash()
has the following body:
private static int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
This effectively recalculates the hash by executing bit operations on the supplied hashcode. I'm unable to understand the need to do so even though the API states it as follows:
This is critical because HashMap uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits.
I do understand that the key value pars are stored in an array of data structures, and that the index location of an item in this array is determined by its hash. What i fail to understand is how would this function add any value to the hash distribution.
Any help would be appreciated!