在阅读HashMap源码时,发现一个奇怪的现象:请看代码
HashMap.java
static final int DEFAULT_INITIAL_CAPACITY = 16;
#HashMap的默认长度是16
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " + loadFactor);
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int) (capacity * loadFactor);
table = new Entry[capacity];
init();
}
上面是HashMap的构造函数,initialCapacity
是容量,loadFactor
是加载因子。但奇怪的是我们传进去的容量不一定是HashMap的实际容量,为什么?看这几句代码
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
局部变量capacity
是HashMap的新的长度,进行位运算,而新的长度capacity
大于initialCapacity
(我们传进去的容量)的最小的2的幂,为什么HashMap的容量必须为2的幂?
所有的一切和这个函数有关:
static int indexFor(int h, int length) {
return h & (length-1);
}
大家知道,HashMap使用数组和链表来实现的,而indexFor
这个函数是根据key
的一系列计算后的值来寻找数组下标。可能大家起疑惑了,这和HashMap的容量必须为2有什么关系?重点在与h&(length-1);
这个语句。为了追求速度,编写者用高效的位运算代替了效率较低的取余运算。但这个代替是有条件的,即h&(length-1)
中的length
必须是2的幂次方。下面是一段测试代码:
public static void main(String[] args) {
Hashtable<Integer, Integer> hashtable = new Hashtable<>();
int value = 113;
for (int i = 0; i < 10000; i++) {
hashtable.put(i & (value - 1), i & (value - 1));
}
for(Iterator<Integer> iterator= hashtable.keySet().iterator();iterator.hasNext();) {
int key = iterator.next();
System.out.println("key = " + key + " value = " + hashtable.get(key));
}
}
以上是我写的一下段代码,测试长度不是2的幂次方(113)的情况下能放几个数,结果很遗憾:
key = 32 value = 32
key = 64 value = 64
key = 96 value = 96
key = 16 value = 16
key = 48 value = 48
key = 80 value = 80
key = 112 value = 112
key = 0 value = 0
如果是2的幂次方的情况,假设是16的情况下:
key = 15 value = 15
key = 14 value = 14
key = 13 value = 13
key = 12 value = 12
key = 11 value = 11
key = 10 value = 10
key = 9 value = 9
key = 8 value = 8
key = 7 value = 7
key = 6 value = 6
key = 5 value = 5
key = 4 value = 4
key = 3 value = 3
key = 2 value = 2
key = 1 value = 1
key = 0 value = 0
看到这里可能明白了,如果不是2的幂次方,存储空间变会少,造成大量空间被浪费掉。所以,无论是HashMap的初始化,还是默认值,都是2的幂次方。