特例说明:
TreeMap、TreeSet两个类在加入第二个元素时,会调用Comparator比较器比较先后加入的元素是否重复(TreeMap比较的是Key值)。所以当加入第一个元素时,即使第一个元素是null,也不会报错,因为此时不会调用比较器,再次加入元素则报错。
已测试的其他集合类HashSet / HashMap / ArrayList / LinkedList均可接受null值。
[java] view plain copy package com.java1029.collection; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; public class TestNull { public static void main(String[] args) { //HashMap 允许null-null键值对 Map<String,String> hashMap = new HashMap<String,String>(); hashMap.put("11", "ddd"); hashMap.put("1233", null); hashMap.put(null, "wang"); hashMap.put(null, null); System.out.println("HashMap以上代码运行成功"); //TreeMap 允许value值为null,不允许key值为null TreeMap<String,String> treeMap = new TreeMap<String,String>(); //Map放入第一个元素时不会调用比较器,所以不会调用比较器,不会出现NullPointerException //以下一行代码执行时不会报错,但当treeMapp中放入元素大于1时,就会调用比较器,出现NullPointerException // treeMap.put(null, null); treeMap.put("ddd", null); treeMap.put("sss", null); System.out.println("TreeMap以上代码运行成功"); //HashSet Set<String> hashSet = new HashSet<String>(); hashSet.add(null); hashSet.add("ddd"); System.out.println("HashSet以上代码运行成功"); //TreeSet Set<String> treeSet = new TreeSet<String>(); //以下两行代码执行时,会报错。理由同TreeMap //treeSet.add(null); treeSet.add("sss"); System.out.println("TreeSet以上代码运行成功"); //ArrayList List<String> arrayList = new ArrayList<String>(); arrayList.add(null); arrayList.add("dd"); System.out.println("ArrayList以上代码运行成功"); //LinkedList List<String> linkedList = new LinkedList<String>(); linkedList.add(null); linkedList.add("ddd"); System.out.println("LinkedList以上代码运行成功"); } } 来源: <http://blog.csdn.net/ml1990s/article/details/11649237>