Skip to content

Latest commit

 

History

History
141 lines (104 loc) · 3.72 KB

19. WeakHashMapIdentityHashMapEnumMap.md

File metadata and controls

141 lines (104 loc) · 3.72 KB

19. WeakHashMap/IdentityHashMap/EnumMap

==引用分类==:

==强引用==StrongReference:引用指向对象,gc运行时不回收

==软引用==SoftReference:gc运行时可能回收(jvm内存不够时)

==弱引用==WeakReference:gc运行时立即回收

==虚引用==PhantomReference:类似于无引用主要跟踪对象被回收的状态,不能单独使用,必须与引用队列(ReferenceQueue)联合使用

package com.alexanderli95.otherCollections.threeReference;

import java.lang.ref.WeakReference;

/**
 * 四类引用:强、软、弱、虚
 */
public class RefDemo {
    public static void main(String[] args) {
        // 存放在堆中,不能共享
        String str=new String("123");
        //弱引用监控对象
        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="123";
        //弱引用监控对象
        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());
    }
}

1. WeakHashMap

键弱引用,回收键后自动删除key-value对象

package com.alexanderli95.otherCollections.threeReference;

import java.util.WeakHashMap;

/**
 * WeakHashMap键为弱类型,运行后立即回收
 */
public class WeakHashMapDemo01 {
    public static void main(String[] args) {
        WeakHashMap<String,String> map=new WeakHashMap<String,String>();
        map.put("1","1");
        map.put("2","2");
        map.put(new String("3"),"3");
        map.put(new String("4"),"4");

        // 通知回收
        System.gc();
        System.runFinalization();

        System.out.println(map.size());  //只留下了前两个,后两个被清理

    }
}

2. IdentityHashMap

键只以地址去重,而不是比较hashcode与equals

==键是常量池中的字符串==

package com.alexanderli95.otherCollections.threeReference;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
    public static void main(String[] args) {
        IdentityHashMap<String,String > identityHashMap=new IdentityHashMap<String ,String >();
        identityHashMap.put("1","1");
        identityHashMap.put("1","2");
        System.out.println(identityHashMap.size());  //只存入了一个,第二个的键地址(常量池中)与第一个相同没有存入

        identityHashMap.put(new String("2"),"1");
        identityHashMap.put(new String("2"),"2");
        System.out.println(identityHashMap.size());  //地址不相同,存入Map
    }
}

3.EnumMap

==键必须是枚举类型对象==

package com.alexanderli95.otherCollections.threeReference;

import java.util.EnumMap;

public class EnumMapDemo {
    public static void main(String[] args) {
        EnumMap<Season,String> enumMap=new EnumMap<Season, String>(Season.class);
        enumMap.put(Season.SPRING,"spring");
        enumMap.put(Season.SUMMER,"summer");
        enumMap.put(Season.AUTUMN,"autumn");
        enumMap.put(Season.WINTER,"winter");

        System.out.println(enumMap.size());
    }
}

enum Season{
    SPRING,SUMMER,AUTUMN,WINTER
}