Java doc说 - 当哈希表中的条目数超过加载因子和当前容量的乘积时,哈希表被重新哈希
在以下程序中 -
HashMap<Integer, String> map = new HashMap<Integer, String>();
int i = 1;
while(i<16) {
map.put(i, new Integer(i).toString());
i++;
}
Run Code Online (Sandbox Code Playgroud)
键是Integer类型,在插入第13到第15个元素时,HashMap容量保持为16,阈值保持与12相同,为什么?
在地图中添加第13个元素后调试屏幕截图 -
args String[0] (id=16)
map HashMap<K,V> (id=19)
entrySet null
hashSeed 0
KeySet null
loadFactor 0.75
modCount 13
size 13
table HashMap$Entry<K,V>[16] (id=25)
threshold 12
values null
i 14
[null, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, 11=11, 12=12, 13=13, null, null]
Run Code Online (Sandbox Code Playgroud)
具有String类型的键的HashMap - HashMap<String, String>或自定义类 - Map<Employee,Integer>显示第13次插入时的预期行为