昨天寫代碼的時(shí)候需要往容器中添加null,所以主要測試了幾個(gè)常見的容器能否添加null的情況系草,如下:
public class TestNull {
public static void main(String[] args) {
//HashMap 允許null-null鍵值對
Map<String,String> hashMap = new HashMap<String,String>();
try {
hashMap.put("test1", "value1");
hashMap.put("test2", null);
hashMap.put(null, "value2");
hashMap.put(null, null);
System.out.println("HashMap添加null-null成功。" + "(hashMap.size() = " + hashMap.size() + ")" );
} catch (Exception e) {
System.out.println("Hash添加失敗。" + "(Error : " + e.toString() + ")");
}
//TreeMap 不允許(null,null)悟耘,允許value為null 會(huì)調(diào)用比較器
TreeMap<String,String> treeMap1 = new TreeMap<String,String>();
try {
treeMap1.put("test1", null);
treeMap1.put("test2", null);
System.out.println("TreeMap添加value為null成功。");
} catch (NullPointerException e) {
System.out.println("TreeMap添加value為null失敗织狐。" + "(Error : " + e.toString() + ")");
}
TreeMap<String,String> treeMap2 = new TreeMap<String,String>();
try {
treeMap2.put(null, null);
System.out.println("TreeMap添加(null,null)成功");
} catch (NullPointerException e) {
System.out.println("TreeMap添加(null,null)失敗暂幼。" + "(Error : " + e.toString() + ")");
}
//TreeSet 不允許 跟TreeMap都是用紅黑樹實(shí)現(xiàn)的 也會(huì)調(diào)用比較器
Set<String> treeSet = new TreeSet<String>();
try {
treeSet.add(null);
System.out.println("TreeSet添加null成功筏勒。");
}catch (NullPointerException e) {
System.out.println("TreeSet添加null失敗。" + "(Error : " + e.toString() + ")");
}
//HashSet
Set<String> hashSet = new HashSet<String>();
try {
hashSet.add(null);
hashSet.add("test");
System.out.println( "HashSet添加null成功旺嬉。" + "(hashSet.size() = " + hashSet.size() + ")" );
}catch (NullPointerException e) {
System.out.println("HashSet添加null失敗管行。" + "(Error : " + e.toString() + ")");
}
//ArrayList
List<String> arrayList = new ArrayList<String>();
try {
arrayList.add(null);
arrayList.add("test");
System.out.println( "ArrayList添加null成功。" + "(arrayLisht.size() = " + arrayList.size() + ")" );
} catch (NullPointerException e) {
System.out.println("ArrayList添加null失敗邪媳。" + "(Error : " + e.toString() + ")");
}
//LinkedList
List<String> linkedList = new LinkedList<String>();
try {
arrayList.add(null);
arrayList.add("test");
System.out.println( "LinkedList添加null成功捐顷。" + "(linkedList.size() = " + linkedList.size() + ")" );
} catch (NullPointerException e) {
System.out.println("LinkedList添加null失敗。" + "(Error : " + e.toString() + ")");
}
Deque<String> arrayDeque = new ArrayDeque<String>();
try {
arrayDeque.addFirst(null);
System.out.println( "ArrayDeque添加null成功雨效。" + "(arrayDeque.size() = " + arrayDeque.size() + ")" );
} catch (Exception e) {
System.out.println("ArrayDeque添加null失敗迅涮。" + "(Error : " + e.toString() + ")");
}
}
}
測試結(jié)果如下:
HashMap添加null-null成功。(hashMap.size() = 3)
TreeMap添加value為null成功徽龟。
TreeMap添加(null,null)失敗叮姑。(Error : java.lang.NullPointerException)
TreeSet添加null失敗。(Error : java.lang.NullPointerException)
HashSet添加null成功据悔。(hashSet.size() = 2)
ArrayList添加null成功传透。(arrayLisht.size() = 2)
LinkedList添加null成功。(linkedList.size() = 0)
ArrayDeque添加null失敗极颓。(Error : java.lang.NullPointerException)