I was going through Java's HashMap source code when I saw the
following //The default initial capacity - MUST be a power of
two. static final int DEFAULT_INITIAL_CAPACITY = 16; My question
is why does this requirement exists in the first place? I also
see that the constructor which allows creating a HashMap with a
custom capacity converts it into a power of two: int capacity =
1; while (capacity < initialCapacity) capacity <<= 1; Why does
the capacity always has to be a power of two? Also, when
automatic rehashing is performed, what exactly happens? Is the
hash function altered too?