WeakHashMap、IdentityHashMap 、EnumMap简单了解——高淇JAVA300讲笔记之其他Map实现类

  

案例一:弱引用管理对象

 1 package com.bjsxt.others.three;
 2 
 3 import java.lang.ref.WeakReference;
 4 
 5 /**
 6  * 引用分类:强、软、弱、虚
 7  * 强与弱引用
 8  *
 9  */
10 public class RefDemo {
11     public static void main(String[] args) {
12         //字符串常量池 存在堆中
13         String str = new String("bjsxt is very good");
14         //弱引用 管理 对象
15         WeakReference<String> wr = new WeakReference<String>(str);
16         System.out.println("gc运行前:"+wr.get());
17         //断开引用
18         str = null;
19         //通知回收
20         System.gc();
21         System.runFinalization();
22         //对象被回收
23         System.out.println("gc运行后:"+wr.get());
24     }
25     
26     public static void testStrong() {
27         //字符串常量池 共享(不能回收)
28         String str = "bjsxt is very good";
29         //弱引用 管理 对象
30         WeakReference<String> wr = new WeakReference<String>(str);
31         System.out.println("gc运行前:"+wr.get());
32         //断开引用
33         str = null;
34         //通知回收
35         System.gc();
36         System.runFinalization();
37         System.out.println("gc运行后:"+wr.get());
38     }
39     
40 }

运行结果:如果执行testStrong(),不会被回收

gc运行前:bjsxt is very good
gc运行后:bjsxt is very good

执行main方法中的代码,会被回收

gc运行前:bjsxt is very good
gc运行后:null

案例二:WeakHashMap示例

 1 package com.bjsxt.others.three;
 2 
 3 import java.util.WeakHashMap;
 4 
 5 /**
 6  * WeakHashMap 键为弱类型,gc运行立即回收
 7  *
 8  */
 9 public class WeakHashMapDemo {
10     public static void main(String[] args) {
11         WeakHashMap<String,String> map = new WeakHashMap<String,String>();
12         //测试数据
13         //常量池对象,不会回收
14         map.put("abc","a");
15         map.put("d","test");
16         //gc运行 已被回收
17         map.put(new String("bjsxt"), "c");
18         map.put(new String("dsf"), "d");
19         
20         //通知回收
21         System.gc();
22         System.runFinalization();
23         
24         System.out.println(map.size());
25     }
26     
27     
28 }

运行结果:2

案例三:IdentityHashMap 键比较地址去重

 1 package com.bjsxt.others.three;
 2 
 3 import java.util.IdentityHashMap;
 4 
 5 /**
 6  * IdentityHashMap 键比较地址去重
 7  *
 8  */
 9 public class IdentityHashMapDemo {
10     public static void main(String[] args) {
11         IdentityHashMap<String,String> map = new IdentityHashMap<String,String>();
12         //常量池中的"a"
13         map.put("a","a1");
14         map.put("a","a2");
15         System.out.println(map.size());
16         map.put(new String("a"), "a3");
17         map.put(new String("a"), "a4");
18         System.out.println(map.size());
19     }
20 }

运行结果:

1
3

案例四:EnumMap要求键为枚举

 1 package com.bjsxt.others.three;
 2 
 3 import java.util.EnumMap;
 4 
 5 /**
 6  * EnumMap要求键为枚举
 7  *
 8  */
 9 public class EnumMapDemo {
10     public static void main(String[] args) {
11         EnumMap<Season,String> map = new EnumMap<Season,String>(Season.class);
12         //存放值
13         map.put(Season.SPRING, "春困");
14         map.put(Season.SUMMER, "夏无力");
15         map.put(Season.AUTUMN, "秋乏");
16         map.put(Season.WINTER, "冬眠");
17         
18         System.out.println(map.size());
19     }
20 }
21 
22 //季节
23 enum Season{
24     SPRING,SUMMER,AUTUMN,WINTER
25 }

运行结果:4

原文地址:https://www.cnblogs.com/swimminglover/p/8353746.html