HashMap implementation in Java. How does the bucket index calculation work?

The hash itself is calculated by the hashCode() method of the object you’re trying to store.

What you see here is calculating the “bucket” to store the object based on the hash h. Ideally, to evade collisions, you would have the same number of buckets as is the maximum achievable value of h – but that could be too memory demanding. Therefore, you usually have a lower number of buckets with a danger of collisions.

If h is, say, 1000, but you only have 512 buckets in your underlying array, you need to know where to put the object. Usually, a mod operation on h would be enough, but that’s too slow. Given the internal property of HashMap that the underlying array always has number of buckets equal to 2^n, the Sun’s engineers could use the idea of h & (length-1), it does a bitwise AND with a number consisting of all 1‘s, practically reading only the n lowest bits of the hash (which is the same as doing h mod 2^n, only much faster).

Example:

     hash h: 11 1110 1000  -- (1000 in decimal)
   length l: 10 0000 0000  -- ( 512 in decimal)
      (l-1): 01 1111 1111  -- ( 511 in decimal - it will always be all ONEs)
h AND (l-1): 01 1110 1000  -- ( 488 in decimal which is a result of 1000 mod 512)

Leave a Comment