可能重复:
Java HashMap 默认初始容量
我正在阅读 java.util.HashMap 中 HashMap 的实现。初始容量、最大容量等是 2 的幂。
从 java.util.HashMap 复制的部分声明
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;
/**
* The maximum capacity, used if a higher value is implicitly specified
* by either of the constructors with arguments.
* MUST be a power of two <= 1<<30.
*/
static final int MAXIMUM_CAPACITY = 1 << 30;
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
transient Entry[] table;
评论表明尺寸必须是 2 的幂。为什么二的力量如此重要?