常用集合存放null

来源:互联网 发布:泰语自学软件 编辑:程序博客网 时间:2024/05/17 01:57

1.List、Set、Map集合:

List arrayList = new ArrayList();        arrayList.add(null);        arrayList.add(null);        arrayList.forEach(System.out::println);        List linkedList = new LinkedList();        linkedList.add(null);        linkedList.add(null);        arrayList.forEach(System.out::println);        HashSet hashSet = new HashSet();        hashSet.add(null);        //hashSet.add(null);//只能添加一个        hashSet.forEach(System.out::println);        HashMap hashMap = new HashMap();        hashMap.put(null, null);        hashMap.forEach((key, value) -> System.out.println(key + ":" + value));        Hashtable hashtable = new Hashtable();        //hashtable.put(null, null);//不能添加        hashtable.forEach((key, value) -> System.out.println(key + ":" + value));

由此可知:
List 集合可以存储多个null;

Set集合也可以存储null,但只能存储一个;

HashMap可以存储null键值对,键和值都可以是null,但如果添加的键值对的键相同,则后面添加的键值对会覆盖前面的键值对,即之后存储后添加的键值对;

Hashtable不能添加null,抛空指针。



原创粉丝点击