集合之WeakHashMap

WeakHashMap
  底层数据结构是哈希表结构
  依赖于键的数据结构特点
  不同于HashMap,该类的键是以弱键的形式存在
  当该键成为垃圾对象,会被垃圾回收期空闲的时候回收,那么改键所对应值也会被回收

 1 WeakHashMap<String, String> whm = new WeakHashMap<>();
 2   
 3 whm.put(new String("hello1"), "world1");
 4 whm.put(new String("hello2"), "world2");
 5 whm.put(new String("hello3"), "world3");//这几个会被回收
 6 whm.put("hello4", "world4");//不会被回收
 7 
 8 System.out.println(whm);
 9 
10 System.gc(); // 启动垃圾回收器
11 System.runFinalization(); // 调用回收器回收垃圾
12 
13 System.out.println(whm);
原文地址:https://www.cnblogs.com/lrxvx/p/9407837.html