Java HashMap 扩容机制探索

    public static void main(String[] args) {
        Map<Integer,Integer> map = new HashMap<>(3);
        print(map);
        System.out.println("--------------------------------");
        for(int i=1; i<55; i++){
            map.put(i, i);
            print(map);
        }
    }

    private static int prevCap = 0;

    private static void print(Map<Integer,Integer> map){
        try{
            Field threshold = map.getClass().getDeclaredField("threshold");
            Field table = map.getClass().getDeclaredField("table");
            threshold.setAccessible(true);
            table.setAccessible(true);

            System.out.print("size:"+map.size()+", threshold:"+threshold.get(map)+", ");

            Object tab = table.get(map);
            int capacity =  tab == null ? 0 : ((Object[])tab).length;
            System.out.print("capacity:"+  capacity);
            if(prevCap != capacity){
                System.out.print("----------扩容");
                prevCap = capacity;
            }
            System.out.println();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
size:0, threshold:4, capacity:0
--------------------------------
size:1, threshold:3, capacity:4----------扩容
size:2, threshold:3, capacity:4
size:3, threshold:3, capacity:4
size:4, threshold:6, capacity:8----------扩容
size:5, threshold:6, capacity:8
size:6, threshold:6, capacity:8
size:7, threshold:12, capacity:16----------扩容
size:8, threshold:12, capacity:16
size:9, threshold:12, capacity:16
size:10, threshold:12, capacity:16
size:11, threshold:12, capacity:16
size:12, threshold:12, capacity:16
size:13, threshold:24, capacity:32----------扩容
size:14, threshold:24, capacity:32
size:15, threshold:24, capacity:32
size:16, threshold:24, capacity:32
size:17, threshold:24, capacity:32
size:18, threshold:24, capacity:32
size:19, threshold:24, capacity:32
size:20, threshold:24, capacity:32
size:21, threshold:24, capacity:32
size:22, threshold:24, capacity:32
size:23, threshold:24, capacity:32
size:24, threshold:24, capacity:32
size:25, threshold:48, capacity:64----------扩容
size:26, threshold:48, capacity:64
size:27, threshold:48, capacity:64
size:28, threshold:48, capacity:64
size:29, threshold:48, capacity:64
size:30, threshold:48, capacity:64
size:31, threshold:48, capacity:64
size:32, threshold:48, capacity:64
size:33, threshold:48, capacity:64
size:34, threshold:48, capacity:64
size:35, threshold:48, capacity:64
size:36, threshold:48, capacity:64
size:37, threshold:48, capacity:64
size:38, threshold:48, capacity:64
size:39, threshold:48, capacity:64
size:40, threshold:48, capacity:64
size:41, threshold:48, capacity:64
size:42, threshold:48, capacity:64
size:43, threshold:48, capacity:64
size:44, threshold:48, capacity:64
size:45, threshold:48, capacity:64
size:46, threshold:48, capacity:64
size:47, threshold:48, capacity:64
size:48, threshold:48, capacity:64
size:49, threshold:96, capacity:128----------扩容
size:50, threshold:96, capacity:128
size:51, threshold:96, capacity:128
size:52, threshold:96, capacity:128
size:53, threshold:96, capacity:128
size:54, threshold:96, capacity:128

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值