List.of and Arrays.asList difference

Arrays.asList return variable list, and returns the List.of immutable List

List <Integer> Arrays.asList List = (. 1, 2, null);
list.set (. 1, 10); // the OK
List <Integer> List.of List = (. 1, 2,. 3);
list.set (. 1, 10); // Fails

    Arrays.asList support null, and List.of not

List <Integer> list = Arrays.asList ( 1 , 2, null); // the OK
List <Integer> List.of List = (. 1, 2, null); // exception: NullPointerException


    thereof contains not the same as the processing method null

List <Integer> list = Arrays.asList ( . 1, 2,. 3);
list.contains (null); the Return to false //
List <Integer> List.of List = (. 1, 2,. 3);
list.contains (null); // NullPointerException is thrown


    Arrays. asList: modifying an array will affect the original array.

Integer [] Array = {l, 2,3};
List <Integer> = Arrays.asList List (Array);
array[1] = 10;
System.out.println(list); // 输出 [1, 10, 3]
Integer[] array = {1,2,3};
List<Integer> list = List.of(array);
array[1] = 10;
System.out.println(list); // 输出 [1, 2, 3]

Published 24 original articles · won praise 2 · Views 344

Guess you like

Origin blog.csdn.net/qq_42107430/article/details/104025252