java10 WeakHashMap

WeakHashMap:
对象所占用的区域是不能直接操作的,都是通过引用来操作。

引用分类:
1.强引用(StrongReference):gc(垃圾回收机制)运行时不回收。例如字符串常量池。字符串虽然你不用了,但是别人可能使用,字符串常量池是可以共享的,因此不能回收。
2.软引用(SoftReference):gc运行的时候可能回收也可能不回收,jvm内存不够的时候才回收。软引用可用于制作缓存,常用的数据不需要经常获取,把它缓存下来,jvm内存不够的时候才把这个对象占用的区域进行回收。
3.弱引用(WeakReference):gc运行的时候立即回收。
4.虚引用(Phantomreference):类似于无引用,没有引用,这个引用不能单独使用,只要用于跟踪对象被回收的状态,比如回收之前把这个对象进行获取,不能单独使用,必须与引用队列(ReferenceQueue)联合使用。




WeakHashMap:
键为弱引用,回收键后自动删除key-value对象。
import java.lang.ref.WeakReference;
/**
 * 引用分类:强、软、弱、虚
 * 强与弱引用
 */
public class RefDemo {
    public static void main(String[] args) {
        //"bjsxt is very good"存放在字符串常量池中,是共享的, 不能回收。
        String str = "bjsxt is very good";
        //弱引用 管理 对象
        WeakReference<String> wr =new WeakReference<String>(str);
        System.out.println("gc运行前:"+wr.get());//gc运行前:bjsxt is very good
        //断开引用
        str =null;
        //通知回收
        System.gc();
        //开始回收
        System.runFinalization();
        System.out.println("gc运行后:"+wr.get()); //gc运行后:bjsxt is very good,所以常量池的对象不能回收。如果改为String str = new String("bjsxt is very good");则第二次输出为null.
    }
}



import java.util.WeakHashMap;
/**
 * WeakHashMap 键为弱类型,gc运行立即回收,回收键后自动删除key-value对象。
 */
public class WeakHashMapDemo {
    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());//2
        System.out.println(map.get("abc"));//a
        System.out.println(map.get("d"));//test
        System.out.println(map.get("bjsxt"));//null,不回收则输出c
        System.out.println(map.get("dsf"));//null,不回收则输出d
        //WeakHashMap的作用,可以回收内存。
    }

}





IdentityHashMap:键以地址来区分是不是同一个,与HashMap不同,HashMap比较的是key的hashcode和equals。


/**
 * IdentityHashMap 键比较地址来去重
 */
public class IdentityHashMapDemo {
    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());//1
        map.put(new String("a"), "a3");
        map.put(new String("a"), "a4");
        System.out.println(map.size());//3
    }
}



EnumMap:
键必须为枚举的值(枚举就是常量的集合)。
构造器:public EnumMap(指定枚举class对象)。
/**
 * EnumMap要求键为枚举
 */
public class EnumMapDemo {
    public static void main(String[] args) {
        EnumMap<Season,String> map =new EnumMap<Season,String>(Season.class);//Season.class为枚举class对象
        //存放值
        map.put(Season.SPRING, "春困");
        map.put(Season.SUMMER, "夏无力");
        map.put(Season.AUTUMN, "秋乏");
        map.put(Season.WINTER, "冬眠");
        System.out.println(map.size());//4
    }
}
//季节
enum Season{
    SPRING,SUMMER,AUTUMN,WINTER
}






同步控制与只读设置:
同步控制:多线程里面对这个访问的对象要保证线程安全,主要是容器和集合的线程安全。
常用容器ArrayList、HashSet、HashMap都不是线程安全的(Hashtable是线程安全的,Vector是线程安全的)。Collections提供了synchronizedXxx()方法,将指定容器包装成同步synchronizedList()、synchronizedSet()、synchronizedMap().
/**
 * 使用Collections管理同步 容器
 * synchronizedList()
    synchronizedSet()
    synchronizedMap()
 */
public class Demo01 {
    public static void main(String[] args) {
        List<String> list =new ArrayList<String>();
        list.add("a");
        list.add("b");
        //list可以同步
        List<String> synList =Collections.synchronizedList(list);//这样list就是线程安全了。
        System.out.println("线程安全的list制作完毕");
    }
}


/**
 只读设置
1、emptyXxx()  空的不可变的集合 
emptyList() 
emptyMap()
emptySet()

2、singletonXxx() 一个元素且不可变的集合
singleton(T o) 
singletonList(T o) 
singletonMap(K key, V value) 

3、unmodifiableXxx() 不可变容器
unmodifiableList(List<? extends T> list) 
unmodifiableSet(Set<? extends T> s) 
unmodifiableMap(Map<? extends K,? extends V> m) 
 */
public class Demo02 {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("test", "test");
        map.put("bjsxt", "bjsxt");
        //控制只读
        Map<String,String> map2 = Collections.unmodifiableMap(map);
        //map2.put("a", "a"); //报错。不能增操作
        System.out.println(map2.size());
        
        //一个元素的容器测试
        String s = new String();//空字符串
        List<String> list = Collections.singletonList(new String());
        list.add("test");//报错,不能操作,已经有了一个元素(空串)
        list.add("bjsxt"); //只能包含一个元素的容器,不能增加。
    }
    
    public static Set<String> oper(Set<String> set){
        if(null==set){
            return Collections.EMPTY_SET; //外部获取避免NullPointerException
        }
        //操作
        return set;
    }
}

 

posted @ 2015-09-23 21:27  无天666  阅读(285)  评论(0编辑  收藏  举报