Java HashMap 扩容机制探索

Java HashMap 扩容机制探索,第1张

Java HashMap 扩容机制探索
    public static void main(String[] args) {
        Map 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 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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存