集合框架中的map集合

**1、集合框架中的map接口有四个继承类分别为

**HashMap.HashTable.LinkedHashMap,TreeMap;

2HashMap.HashTable.,TreeMap;各自的特点**

2.1 HashMap的特点

  2..1.1允许多个null值和一个Null键;(即使放入多个null键位也会被后面所添加的覆盖掉)
Map<Object, Object> map=new HashMap<>();
	   map.put(null, null);
	   map.put(null, 123);
	   Set<Entry<Object, Object>> entrySet = map.entrySet();
	   for (Entry<Object, Object> entry : entrySet) {
		   Object key = entry.getKey();
		   Object value = entry.getValue();
		System.out.println("键位:"+key+",值位:"+value);
	}	

输出的结果为键位:null,值位:123

2.1.2.HashMap中的元素没有顺序(跟添加的顺序无关);
略…
2.1.3.HashMap不是线程安全的(即为异步,但HashMap的异步问题可通过Collections的一个静态方法得到解决,Map Collections.synchronizedMap(Map m))。

2.2 HsahTable的特点

2.2.1不允许任何的null键和值;

Map<Object, Object> map=new Hashtable<>();
	   map.put(123, null);
	   map.put(1123, 123);
	   map.put(234, "sa");
	   map.put(123, "asd");
	   map.put(null, 25);
	   map.put(567, "dfhg");
	   Set<Entry<Object, Object>> entrySet = map.entrySet();
	   for (Entry<Object, Object> entry : entrySet) {
		   Object key = entry.getKey();
		   Object value = entry.getValue();
		System.out.println("键位:"+key+",值位:"+value);
	}	

输出为Exception in thread "main" java.lang.NullPointerException at java.util.Hashtable.put(Hashtable.java:459) at jee02.Test.main(Test.java:107)
2.2.2HashTable中的元素没有顺序;

2.2.3HashTable是线程安全的。(即为同步)

2.3 TreeMap集合的特点

2.3.1.可以按着KEY来排序(如果你想要用你自己创建的类去当键排序,那么你需要在你自己的类中实现Serializable, Comparable<Student>两个接口中的方法并且map中的键位类型要一致(如果是自己的类的话));

Map<Object, Student> set=new TreeMap<>();
		set.put("sa",new Student(1, "sa", "m"));
		set.put("sd",new Student(5, "sb", "f"));
		set.put("aa",new Student(3, "ss", "m"));
		Set<Entry<Object, Student>> entrySet2 = set.entrySet();
		for (Entry<Object, Student> entry : entrySet2) {
			Student value = entry.getValue();
			Object key = entry.getKey();
			System.out.println("键位:"+key+",值位:"+value);
		}

运行如下

键位:aa,值位:Student [sid=3, sname=ss, sex=m]
键位:sa,值位:Student [sid=1, sname=sa, sex=m]
键位:sd,值位:Student [sid=5, sname=sb, sex=f]

Map<Object, Student> set=new TreeMap<>();
		set.put(new Student(1, "sa", "m"),new Student(1, "sa", "m"));
		set.put(new Student(3, "sa", "m"),new Student(5, "sb", "f"));
		set.put(new Student(2, "sa", "m"),new Student(3, "ss", "m"));
		Set<Entry<Object, Student>> entrySet2 = set.entrySet();
		for (Entry<Object, Student> entry : entrySet2) {
			Student value = entry.getValue();
			Object key = entry.getKey();
			System.out.println("键位:"+key+",值位:"+value);
		}

输出为键位:Student [sid=1, sname=sa, sex=m],值位:Student [sid=1, sname=sa, sex=m] 键位:Student [sid=2, sname=sa, sex=m],值位:Student [sid=3, sname=ss, sex=m] 键位:Student [sid=3, sname=sa, sex=m],值位:Student [sid=5, sname=sb, sex=f]

2.3.2.KEY不能是null、不能重复,值可以有多个null;
实例

Map<Object, Student> set=new TreeMap<>();
		set.put(null,new Student(1, "sa", "m"));
		set.put(12,null);
		set.put(14,null);
		Set<Entry<Object, Student>> entrySet2 = set.entrySet();
		for (Entry<Object, Student> entry : entrySet2) {
			Student value = entry.getValue();
			Object key = entry.getKey();
			System.out.println("键位:"+key+",值位:"+value);
		}

输出为

Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1294) at java.util.TreeMap.put(TreeMap.java:538) at jee02.Test.main(Test.java:39)

2.3.3.不是线程安全的.(异步)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值