What is the difference between List.of and Arrays.asList?

user5908494 :

Java 9 introduced new factory methods for lists, List.of:

List<String> strings = List.of("first", "second");

What's the difference between the previous and the new option? That is, what's the difference between this:

Arrays.asList(1, 2, 3);

and this:

List.of(1, 2, 3);
ZhekaKozlov :

Arrays.asList returns a mutable list while the list returned by List.of is immutable:

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

List<Integer> list = List.of(1, 2, 3);
list.set(1, 10); // Fails with UnsupportedOperationException

Arrays.asList allows null elements while List.of doesn't:

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

contains behaves differently with nulls:

List<Integer> list = Arrays.asList(1, 2, 3);
list.contains(null); // Returns false

List<Integer> list = List.of(1, 2, 3);
list.contains(null); // Fails with NullPointerException

Arrays.asList returns a view of the passed array, so the changes to the array will be reflected in the list too. For List.of this is not true:

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

Integer[] array = {1,2,3};
List<Integer> list = List.of(array);
array[1] = 10;
System.out.println(list); // Prints [1, 2, 3]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java 8 - Initilize a List in one line difference between Arrays.asList and Stream.of

Difference between Arrays.asList(array) and new ArrayList<Integer>(Arrays.asList(array))

What is the best way of using Arrays.asList() to initialize a List

What is the difference between these 2 ways of creating a list of arrays in JavaScript?

What is the difference between slicing in numpy arrays and slicing a list in Python?

What is the difference between these 2 arrays

Initialize List<> with Arrays.asList

What's the difference between list() and []

What is the difference between Set and List?

What is the difference between quote and list?

What is the difference between List and ArrayList?

What is the difference between [[]] and $ in list indexing?

What is the difference between Array and Arrays class in Java?

What the difference between Ruby + and concat for arrays?

What is the difference between the values of these two char arrays?

Regarding immutable List (created by Arrays.asList())

ArrayList vs the List returned by Arrays.asList()

What is the difference between a list of tuples and a list of objects?

What is the difference between list += str and list += str,

What is the difference between ('a * 'a) list and 'a * 'a list in ocaml?

What is the difference between list and list[:] in python?

What's the difference between List<Object> and List<?>

What is the difference between List<Object[]> and List<Object>?

What's the difference between list[::] and list?

Extended Glob: What is the difference in syntax between ?(list), *(list), +(list) and @(list)

What is the difference between List.view and LazyList?

What is the difference between these two List in Python?

What is the difference between list.files and dir?

What is the difference between Collection and List in Java?