-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestNull.java
More file actions
62 lines (50 loc) · 2.35 KB
/
TestNull.java
File metadata and controls
62 lines (50 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package _Test;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class TestNull {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(null);
list.add(null);
System.out.println("ArrayList :" + list.size()); //2
List<Integer> linkedList = new LinkedList<>();
linkedList.add(null);
linkedList.add(null);
System.out.println("LinkedList :" + linkedList.size()); //2
Set<Integer> set = new HashSet<>();
set.add(null);
set.add(null);
System.out.println("HashSet :" + set.size());// 1
Set<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(null);
linkedHashSet.add(null);
System.out.println("LinkedHashSet :" + linkedHashSet.size()); //1
TreeSet<String> treeSet = new TreeSet<>();
//treeSet.add(null); //treeSet key不能null ava.lang.NullPointerException
//treeSet.add(null);
System.out.println("TreeSet : " + treeSet.size());
HashMap<String, String> map = new HashMap<>();
map.put(null, null);
map.put(null, "aaa"); // 相同key, value被覆盖
map.put("aaa", null);
System.out.println("HashMap :" + map.size());// 2
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(null, null);
linkedHashMap.put("aaa", null);
System.out.println("LinkedHashMap :" + linkedHashMap.size()); //2
TreeMap<String, String> treeMap = new TreeMap<>();
treeMap.put("aaa", null); //treeMap key 不能为空,value 可以为空
//treeMap.put(null,"aaa");
//treeMap.put(null,null);
System.out.println("TreeMap :" + treeMap.size());
Hashtable<String, String> hashtable = new Hashtable<>();
// hashtable.put("aaa",null); // hashtable key与value均不能为空
// hashtable.put(null,"aaa");
// hashtable.put(null, null);
System.out.println("Hashtable" + hashtable.size());
ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
concurrentHashMap.put("aaa", null); // concurrentHashmap key 与 value 均不可以为空
//concurrentHashMap.put(null,"aaa");
System.out.println("ConcurrentHashMap :" + concurrentHashMap.size());
}
}