admin管理员组文章数量:1611924
1.HashMap
(1)size和capacity
size:表示HashMap中已经装入多少个元素
capacity表示HashMap中可以最多容纳多少个元素
其中capacity默认的大小是16,设计成16主要是可以使用按位与替代取模来提升hash的效率。
代码验证:
Map<String,String> map = new HashMap<String, String>();
map.put("test", "testValue");
Class<?> mapType = map.getClass();
Method capacity = null;
try {
capacity = mapType.getDeclaredMethod("capacity");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
capacity.setAccessible(true);
try {
System.out.println("capacity : " + capacity.invoke(map));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Field size = null;
try {
size = mapType.getDeclaredField("size");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
size.setAccessible(true);
try {
System.out.println("size : " + size.get(map));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
(2)loadFactor和threshold
loadFactor:负载因子,默认值为0.75
threshold = capacity * loadFactor;
当map所容纳的数量达到threshold的值时,hashMap就会自动扩容(JDK1.7是不能反映在capacity上,只有当容纳个数大于capacity时,才会反映在capacity上;
但是实际中threshold的值已经发生改变,当容纳值大于12时。JDK1.8以上都会在容纳值大于12时,反映在capacity和threshold上)
通过下面的代码对比可以发现,hashMap进行扩容应该是当量达到capacity的大小时才会进行扩容(代码所用JDK1.7)
Map<String, String> map = new HashMap<String, String>();
map.put("test1", "test");
map.put("test2", "test");
map.put("test3", "test");
map.put("test4", "test");
map.put("test5", "test");
map.put("test6", "test");
map.put("test7", "test");
map.put("test8", "test");
map.put("test9", "test");
map.put("test10", "test");
map.put("test11", "test");
map.put("test12", "test");
Class<?> mapType = map.getClass();
Method capacity = mapType.getDeclaredMethod("capacity");
capacity.setAccessible(true);
System.out.println("capacity : " + capacity.invoke(map));
Field size = mapType.getDeclaredField("size");
size.setAccessible(true);
System.out.println("size : " + size.get(map));
Field threshold = mapType.getDeclaredField("threshold");
threshold.setAccessible(true);
System.out.println("threshold : " + threshold.get(map));
Field loadFactor = mapType.getDeclaredField("loadFactor");
loadFactor.setAccessible(true);
System.out.println("loadFactor : " + loadFactor.get(map));
结果:
capacity : 16
size : 12
threshold : 12
loadFactor : 0.75
===========================================================
Map<String, String> map = new HashMap<String, String>();
map.put("test1", "test");
map.put("test2", "test");
map.put("test3", "test");
map.put("test4", "test");
map.put("test5", "test");
map.put("test6", "test");
map.put("test7", "test");
map.put("test8", "test");
map.put("test9", "test");
map.put("test10", "test");
map.put("test11", "test");
map.put("test12", "test");
map.put("test13", "test");
map.put("test14", "test");
map.put("test15", "test");
map.put("test16", "test");
map.put("test17", "test");
Class<?> mapType = map.getClass();
Method capacity = mapType.getDeclaredMethod("capacity");
capacity.setAccessible(true);
System.out.println("capacity : " + capacity.invoke(map));
Field size = mapType.getDeclaredField("size");
size.setAccessible(true);
System.out.println("size : " + size.get(map));
Field threshold = mapType.getDeclaredField("threshold");
threshold.setAccessible(true);
System.out.println("threshold : " + threshold.get(map));
Field loadFactor = mapType.getDeclaredField("loadFactor");
loadFactor.setAccessible(true);
System.out.println("loadFactor : " + loadFactor.get(map));
结果:
capacity : 32
size : 17
threshold : 24
loadFactor : 0.75
本文标签: sizeHashMaploadFactorCapacitythreshold
版权声明:本文标题:HashMap中size-loadfactor-threshold-capacity 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728621667a1166431.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论