1.HashMap默认容量大小是16,装载因子是0.75,所以 扩容的连接值就是0.75*16=12,当HashMap中的大小超过12 的时候,HashMap就会自动增加一倍,变成32,依次类推。下面是获取扩容的大小以及实现
public void testMap() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
HashMap<String, Object> map = new HashMap<>();
for (int i = 0; i <=12 ; i++) {
map.put(“a”+i,i);
}
Class<? extends HashMap> aClass = map.getClass();
Method capacity = aClass.getDeclaredMethod(“capacity”);
capacity.setAccessible(true);
System.out.println(capacity.invoke(map));
// 当小于12的时候是 16,等于12的时候是32
}
HashMap的扩容
最新推荐文章于 2024-03-12 21:35:16 发布