HashMap中size-loadfactor-threshold-capacity

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值