Java中List.of()和Arrays.asList()的區別及原因
動手寫一下,讓自己更有印象
1.Arrays.asList()可以插入null
而List.of()不可以
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { List<Integer> ls1 = Arrays.asList( 1 , 2 , null ); //List<Integer> ls2 = List.of(1,2,null); System.out.println(ls1); //System.out.println(ls2); } } /*結果 [1, 2, null] */ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { //List<Integer> ls1 = Arrays.asList(1, 2, null); List<Integer> ls2 = List.of( 1 , 2 , null ); //System.out.println(ls1); System.out.println(ls2); } } /*結果 Exception in thread "main" java.lang.NullPointerException..... */ |
2.用List.of的List自然是不包含null
而用Arrays.asList的List包含null。上面結果也可得知。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { List<Integer> ls1 = Arrays.asList( 1 , 2 , null ); //List<Integer> ls2 = List.of(1,2); System.out.println(ls1.contains( null )); //System.out.println(ls2.contains(null)); } } /*結果 true */ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { //List<Integer> ls1 = Arrays.asList(1, 2, null); List<Integer> ls2 = List.of( 1 , 2 ); //System.out.println(ls1.contains(null)); System.out.println(ls2.contains( null )); } } /*結果 Exception in thread "main" java.lang.NullPointerException..... */ |
3.List.of生成的List不能修改
Arrays.asList生成的List能修改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { List<Integer> ls1 = Arrays.asList( 1 , 2 , null ); //List<Integer> ls2 = List.of(1,2); ls1.set( 0 , 5 ); //ls2.set(0,5); System.out.println(ls1); //System.out.println(ls2); } } /*結果 [5, 2, null] */ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { //List<Integer> ls1 = Arrays.asList(1, 2, null); List<Integer> ls2 = List.of( 1 , 2 ); //ls1.set(0,5); ls2.set( 0 , 5 ); //System.out.println(ls1); System.out.println(ls2); } } /*結果 Exception in thread "main" java.lang.UnsupportedOperationExceptio..... */ |
4.關于數組修改對List的影響
數組修改對Arrays.asList生成的List有影響,對List.of 生成的List無影響
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { Integer[] a = new Integer[]{ 1 , 2 , 3 , 4 }; // 不能用int[],會導致轉型錯誤,錯誤: 不兼容的類型: 推論變量 T 具有不兼容的上限 List<Integer> ls1 = Arrays.asList(a); //List<Integer> ls2 = List.of(a); System.out.println(ls1); //System.out.println(ls2); a[ 0 ] = 5 ; //ls2.set(0,5); System.out.println(ls1); //System.out.println(ls2); } } /*結果 [1, 2, 3, 4] [5, 2, 3, 4] */ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import java.util.List; import java.util.Arrays; class Solution { public static void main(String[] args) { Integer[] a = new Integer[]{ 1 , 2 , 3 , 4 }; //List<Integer> ls1 = Arrays.asList(a); List<Integer> ls2 = List.of(a); //System.out.println(ls1); System.out.println(ls2); a[ 0 ] = 5 ; //ls2.set(0,5); //System.out.println(ls1); System.out.println(ls2); } } /*結果 [1, 2, 3, 4] [1, 2, 3, 4] */ |
原因
關于List.of為什么不能插入null,和修改了原數組不影響到List.of生成的List。先看看List.of有關的源碼
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
|
@SafeVarargs @SuppressWarnings ( "varargs" ) static <E> List<E> of(E... elements) { switch (elements.length) { // implicit null check of elements case 0 : return ImmutableCollections.emptyList(); case 1 : return new ImmutableCollections.List12<>(elements[ 0 ]); case 2 : return new ImmutableCollections.List12<>(elements[ 0 ], elements[ 1 ]); default : return new ImmutableCollections.ListN<>(elements); } } //--------------------------------------------------------------------------------------- @Stable private final E[] elements; @SafeVarargs ListN(E... input) { // copy and check manually to avoid TOCTOU @SuppressWarnings ( "unchecked" ) E[] tmp = (E[]) new Object[input.length]; // implicit nullcheck of input for ( int i = 0 ; i < input.length; i++) { tmp[i] = Objects.requireNonNull(input[i]); } elements = tmp; } //--------------------------------------------------------------------------------------- public static <T> T requireNonNull(T obj) { if (obj == null ) throw new NullPointerException(); return obj; } |
可以看到Objects.requireNonNull()。所以不能插入空值。
E[] tmp = (E[])new Object[input.length];表示新建了個新的數組對象,所以修改了原數組,不影響生成的LIst底層的數組。
返回的數組是個final類型的,所以不能修改
再看看Arrays.asList源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@SafeVarargs @SuppressWarnings ( "varargs" ) public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } //---------------------------------------------------------------------------------------- ArrayList(E[] array) { a = Objects.requireNonNull(array); } //---------------------------------------------------------------------------------------- public static <T> T requireNonNull(T obj) { if (obj == null ) throw new NullPointerException(); return obj; } |
由源碼可知,底層的數組就是傳入的數組,所以對原數組的修改會影響到用Arrays.asList方法生成的List。而且Objects.requireNonNull(array)檢查的是整個數組是不是null,而非對每個元素進行檢查是否為null。所以用Arrays.asList方法可以插入空值。
也沒有規定是final的,所以支持修改。
java listof報錯處理
List.of()生成不可變數組(字符串也行)
是在jdk1.8以后才出現的,在jdk1.9版本及以后才能運行。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_42520962/article/details/109380430