Java中List.of()和Arrays.asList()的區(qū)別及原因分析
Java中List.of()和Arrays.asList()的區(qū)別及原因
動手寫一下,讓自己更有印象
1.Arrays.asList()可以插入null
而List.of()不可以
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] */
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。上面結果也可得知。
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 */
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能修改。
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] */
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.關于數(shù)組修改對List的影響
數(shù)組修改對Arrays.asList生成的List有影響,對List.of 生成的List無影響
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] */
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,和修改了原數(shù)組不影響到List.of生成的List。先看看List.of有關的源碼
@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];表示新建了個新的數(shù)組對象,所以修改了原數(shù)組,不影響生成的LIst底層的數(shù)組。
返回的數(shù)組是個final類型的,所以不能修改
再看看Arrays.asList源碼
@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; }
由源碼可知,底層的數(shù)組就是傳入的數(shù)組,所以對原數(shù)組的修改會影響到用Arrays.asList方法生成的List。而且Objects.requireNonNull(array)檢查的是整個數(shù)組是不是null,而非對每個元素進行檢查是否為null。所以用Arrays.asList方法可以插入空值。
也沒有規(guī)定是final的,所以支持修改。
java listof報錯處理
List.of()生成不可變數(shù)組(字符串也行)
是在jdk1.8以后才出現(xiàn)的,在jdk1.9版本及以后才能運行。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
由ArrayList來深入理解Java中的fail-fast機制
fail-fast俗稱快速失敗,是在多線程進行迭代操作時產(chǎn)生沖突的一種異常拋出機制,下面我們就由ArrayList來深入理解Java中的fail-fast機制.2016-05-05解決mybatis-plus3.4.1分頁插件PaginationInterceptor和防止全表更新與刪除插件SqlE
這篇文章給大家介紹了在Spring.xml文件中配置mybatis-plus3.4.1分頁插件PaginationInterceptor和防止全表更新與刪除插件SqlExplainInterceptor過時失效問題解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-12-12Spring應用中使用acutator/refresh刷新屬性不生效的問題分析及解決
在Spring應用收到/actuator/refresh的POST請求后,標注了@RefreshScope以及@ConfiguratioinProperties的bean會被Spring容器重新加載,但是,在實際應用中,并沒有按照預期被Spring容器加載,本文將討論導致這種未按預期刷新的一種原因,感興趣的朋友可以參考下2024-01-01