Hashmap 的 loadFactor设置初使化值的时候必须设置 loadFactor
1、请查看 hashmap 如何初使化
代码:
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);
// Find a power of 2 >= initialCapacity
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int)(capacity * loadFactor);
table = new Entry[capacity];
init();
}
当
Map map = new HashMap(9);
while (capacity < initialCapacity)
capacity <<= 1;
capacity的容量为 16
threshold扩容阀值 16*0.75 =12
和9没什么关系 哈
当
Map map = new HashMap(9,1);
capacity的容量为 16
threshold扩容阀值 16*1 =16
不过这个 9 变小一点 就不一样了 如3时capacity 是4