For a HashMap index in the array that stores the Map entries is calculated this way (where h calculated from the hashCode key):
static int indexFor(int h, int length) { return h & (length-1); }
Where length is the length of the array.
This only works when length is a power of 2. If length not power 2, you will have to change this code to a less efficient return h % length .
source share