Regarding why Hashmap expansion is double expansion, the capacity is always 2 to the power of n.

if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);

Because Hashmap uses (n-1) & hash when calculating the storage location. Only when the capacity n is a power of 2, the binary of n-1 will all be 1, and the bit operation can be fully hashed to avoid unnecessary hash collisions, so the expansion must be doubled in order to maintain the capacity to always be 2. Power.

Guess you like

Origin blog.csdn.net/qq_43518425/article/details/114379391