您的位置:首页 > 编程语言 > Java开发

JAVA学习笔记45——四种引用+三种HashMap+同步控制+不可变设置

2015-02-25 10:11 281 查看
最近在看JAVA教学的视频,觉得老师讲的很好,同时借用源代码还有笔记来撰写本系列博客,记录自己的学习内容,同时也供看到的人学习。

由于过年,已经整整八天没有写博客了,今天继续~

首先来介绍四种引用(references):



从上面的内容可以看出四种不同的引用对应着各自的功能,用于针对不同回收需求的数据而分别设立的,下面看实例代码:

import java.lang.ref.WeakReference;
/**
* 引用分类:强、软、弱、虚
* 强与弱引用
* @author Administrator
*
*/
public class RefDemo {
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(){
//字符串常量池  共享(不能回收),但是如果是new出来的则是共享的,就可以回收了
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());
}
}
接下来介绍三种HashMap,对应gc不同的回收方式,用于不同情况下的数据删除:



import java.util.WeakHashMap;
/**
* WeakHashMap 键为弱类型,gc运行立即回收
* @author Administrator
*
*/
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());
}
}
import java.util.IdentityHashMap;
/**
* IdentityHashMap 键比较地址去重
* @author Administrator
*
*/
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());
//下面的两个“a”地址不同,而IdentityHashMap只是比较地址
map.put(new String("a"), "a3");
map.put(new String("a"), "a4");
System.out.println(map.size());
}
}
import java.util.EnumMap;
/**
* EnumMap要求键为枚举
*/
public class EnumMapDemo {
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
}
最后的内容是同步控制和不可变设置:



示例代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 使用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);
System.out.println("线程安全的list制作完毕");
}
}
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 只读设置
* emptyXxx()  空的不可变的集合
* 1、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());
//一个元素的容器测试
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;
}
}
可以看出,这一篇blog介绍的内容都是对数据回收以及访问的控制方法,这些在实际应用中是很常见的,同时也常常作为JAVA程序猿招聘的面试题~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: