DAY16:尚学堂高琪JAVA(132~142)其他Map的实现类

本文深入探讨了Java中Map的几种特殊实现类,包括WeakHashMap、IdentityHashMap和EnumMap的特性和使用场景,同时讲解了如何通过Collections工具类实现同步控制和只读设置,提升容器的线程安全性和数据不可变性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其他Map的实现类

一: 引用分类
强引用:Strong

二:3个Map接口的实现类

package weakhashmap;
/*
 * 132  WeakHashMap 键为弱类型,gc运行立即回收
 * */
import java.util.WeakHashMap;
public class WeakHashMapDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WeakHashMap<String, String> map=new WeakHashMap<String,String>();
		//常量对象,不会回收
		map.put("abc", "a");
		map.put("def", "d");
		System.gc();
		System.runFinalization();
		System.out.println(map.size());

		map.put(new String("lzk"),"l");
		map.put(new String("xx"),"x");
		//通知回收
		System.gc();
		System.runFinalization();
		System.out.println(map.size());
		
	}

}

在这里插入图片描述

package weakhashmap;
// 132 IdentityHashMapDemo 键比较地址去重
import java.util.IdentityHashMap;
public class IdentityHashMapDemo {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IdentityHashMap<String,String> map=new IdentityHashMap<String,String>( );
		//常量池中的“a”
		map.put("a", "1");
		map.put("a", "2");
		System.out.println(map.size());
		//堆中的“a”
		map.put(new String("a"), "3");
		map.put(new String("a"), "4");
		System.out.println(map.size());
	}
}

在这里插入图片描述

package weakhashmap;
/*
 * 132EnumMap要求键为枚举
 * */
import java.util.EnumMap;
public class EnumMapDemo {
	public static void main(String[] args) {
		EnumMap<Season,String> map=new EnumMap<Season,String>(Season.class);
		map.put(Season.SPING,"chun");
		map.put(Season.SUMMER,"xia");
		map.put(Season.AUTUMN,"qiu");
		map.put(Season.WINTER,"dong");
		System.out.println(map.size());
		System.out.println(map.get(Season.WINTER));
	}
}
enum Season{
	SPING,SUMMER,AUTUMN,WINTER
}

在这里插入图片描述

同步控制与只读设置

同步控制
1.常用容器ArrayList HashSet HashMap 等都是线程不安全的
2.Collections提供了synchronizedXxx()方法,将指定容器包装成同步

  • synchronizedList()
  • synchronizedSet()
  • synchronizedMap()
    只读设置
    Collections提供了三种方法
  • emptyXxx() ---------空的不可变的集合
  • singletonXxx() ---------一个元素不可变的集合
  • unmodifiableXxx() ---------不可变的容器
package synorread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//133	同步控制与只读设置
public class Demo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list=new ArrayList<String>();
		list.add("a");
		list.add("b");
		//list可以同步
		List<String> synlist=Collections.synchronizedList(list);
		System.out.println("线程安全的list制作完毕");
		System.out.println(synlist);
		
		System.out.println("*****以下为只读控制******");
		Map<String,String> map=new HashMap<String,String>();
		map.put("a", "1");
		map.put("b", "2");
		 //不可变容器测试
		Map<String,String> map2=Collections.unmodifiableMap(map);
		//map2.put("a", "3"); //不能执行这句
		System.out.println(map2.size());
		
		//一个元素的容器测试
		List<String> list3=Collections.singletonList(new String("abc"));
		System.out.println(list3);
		//list3.add("test"); // 不能执行这句 
	}
}

总结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值