132_容器_引用(强软弱虚)_WeakHashMap_IdentityHashMap_EnumMap

这里写图片描述

  • ReferenceDemo.java
package collection.others.Reference;

import java.lang.ref.WeakReference;

/**
 * 引用分类:强、软、弱、虚
 * 强与弱引用
 */

public class ReferenceDemo {
    public static void main(String[] args) {
        //字符串常量池 
        String str =new String("bjsxt is very good");
        //弱引用 管理 对象
        WeakReference<String> wr =new WeakReference<String>(str);
        System.out.println("gc运行前:"+wr.get());
        //断开引用
        str =null;
        //通知回收
        System.gc();
        System.runFinalization();
        //对象被回收
        System.out.println("gc运行后:"+wr.get()); 
    }
    public static void testStrong(){
        //字符串常量池  共享(不能回收)
        String str ="bjsxt is very good";
        //弱引用 管理 对象
        WeakReference<String> wr =new WeakReference<String>(str);
        System.out.println("gc运行前:"+wr.get());
        //断开引用
        str =null;
        //通知回收
        System.gc();
        System.runFinalization();
        System.out.println("gc运行后:"+wr.get());
    }

}
  • TestWeakHashMap.java
package collection.others.Reference;

import java.util.WeakHashMap;

/**
* WeakHashMap 键为弱类型,gc运行立即回收
*/

public class TestWeakHashMap {
    public static void main(String[] args) {
        WeakHashMap<String,String> map =new WeakHashMap<String,String>();
        //测试数据
        //常量池对象,不会回收
        map.put("abc", "a");
        map.put("d", "test");
        //gc运行 已被回收
        map.put(new String("bjsxt"), "c");
        map.put(new String("dsf"), "d");
        //通知回收
        System.gc();
        System.runFinalization();
        System.out.println(map.size());
    }
}
  • TestIdentityHashMap.java
package collection.others.Reference;

import java.util.IdentityHashMap;

/**
 * IdentityHashMap 键比较地址去重
 */

public class TestIdentityHashMap {
    public static void main(String[] args) {
        IdentityHashMap<String ,String> map =new IdentityHashMap<String,String>();
        //常量池中的"a"
        map.put("a", "a1");
        map.put("a", "a2");
        System.out.println(map.size());
        map.put(new String("a"), "a3");
        map.put(new String("a"), "a4");
        System.out.println(map.size());
    }
}
  • TestEnumMap.java
package collection.others.Reference;

import java.util.EnumMap;

/**
 * EnumMap要求键为枚举
 */

public class TestEnumMap {
    public static void main(String[] args) {
        EnumMap<Season,String> map =new EnumMap<Season,String>(Season.class);
        //存放值
        map.put(Season.SPRING, "春困");
        map.put(Season.SUMMER, "夏无力");
        map.put(Season.AUTUMN, "秋乏");
        map.put(Season.WINTER, "冬眠");

        System.out.println(map.size());
    }
}
//季节
enum Season{
    SPRING,SUMMER,AUTUMN,WINTER
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值