WeakHashMap

public class WeakHashMapTest {
public static void main(String[] args) {
    WeakHashMap wMap = new WeakHashMap();
    String p1 = new String("1");
    String p2 = new String("2");    
    String p3 = new String("3");    
    wMap.put(p1, "1");
    wMap.put(p2, "2");
    wMap.put(p3, "3");
    p1=null;
    System.gc();
    /*for (Object o : wMap.keySet()) {
        System.out.println(wMap.get(o));
    }*/
    for (Object o : wMap.entrySet()) {
        System.out.println(o);
    }
}
}

WeakReference是“弱键”实现的哈希表。它这个“弱键”的目的就是:实现对“键值对”的动态回收。当“弱键”不再被使用到时,GC会回收它,WeakReference也会将“弱键”对应的键值对删除。
    “弱键”是一个“弱引用(WeakReference)”,在Java中,WeakReference和ReferenceQueue 是联合使用的。在WeakHashMap中亦是如此:如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。 接着,WeakHashMap会根据“引用队列”,来删除“WeakHashMap中已被GC回收的‘弱键’对应的键值对”。

原文地址:https://www.cnblogs.com/tonggc1668/p/8004080.html