Java基础----hashmap

package com.hashmap;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class HashMapDetail {

    public static void main(String[] args) throws Exception{
        // 初始大小为16,扩容因子为0.75 size为map中的keyvalue对个数
        // 扩容规则为2倍
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < 12; i++) {
            map.put("key" + i, "value" + i);    
        }

        Field loadFactor = HashMap.class.getDeclaredField("loadFactor");
        loadFactor.setAccessible(true);
        System.out.println("loadFactor = " + loadFactor.get(map));

        Field threshold = HashMap.class.getDeclaredField("threshold");
        threshold.setAccessible(true);
        System.out.println("threshold = " + threshold.get(map));

        Field size = HashMap.class.getDeclaredField("size");
        size.setAccessible(true);
        System.out.println("size = " + size.get(map));

        map.put("key13", "value13");
        System.out.println("loadFactor = " + loadFactor.get(map));
        System.out.println("threshold = " + threshold.get(map));
        System.out.println("size = " + size.get(map));
    }
}

猜你喜欢

转载自blog.csdn.net/miracle_8/article/details/79311662