栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java HashMap 扩容机制探索

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709213.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号