Week 123 — What is the difference between List.of() and Arrays.asList()?

Question of the Week #123
What is the difference between List.of() and Arrays.asList()?
2 Replies
dan1st | Daniel
Java provides various factory methods for creating lists and other collections including List.of, Arrays.asList as well as List.copyOf and Collections.unmodifiableList. The List.of method creates a new list from the elements passed as arguments. The created list is immutable.
List<String> immutableList = List.of("Hello", "World");
System.out.println(immutableList.get(0));//Hello
//immutableList.remove(0);//UnsupportedOperationException
//immutableList.set(0, "Goodbye");//UnsupportedOperationException
//immutableList.add("return");//UnsupportedOperationException
List<String> immutableList = List.of("Hello", "World");
System.out.println(immutableList.get(0));//Hello
//immutableList.remove(0);//UnsupportedOperationException
//immutableList.set(0, "Goodbye");//UnsupportedOperationException
//immutableList.add("return");//UnsupportedOperationException
On the other hand, Arrays.asList creates a List view from an array. Changes to the List are reflected in the array and vice-versa. Since arrays are not resizable, it is not possible to add or remove elements.
String[] backingArray = {"Hello", "World"};
List<String> arrayView = Arrays.asList(backingArray);
System.out.println(arrayView.get(0));//Hello
//arrayView.remove(0);//UnsupportedOperationException
arrayView.set(0, "Goodbye");
backingArray[1] = "Simulation";
//arrayView.add("return");//UnsupportedOperationException
System.out.println(arrayView);//[Goodbye, Simulation]
System.out.println(Arrays.toString(backingArray));//[Goodbye, Simulation]
String[] backingArray = {"Hello", "World"};
List<String> arrayView = Arrays.asList(backingArray);
System.out.println(arrayView.get(0));//Hello
//arrayView.remove(0);//UnsupportedOperationException
arrayView.set(0, "Goodbye");
backingArray[1] = "Simulation";
//arrayView.add("return");//UnsupportedOperationException
System.out.println(arrayView);//[Goodbye, Simulation]
System.out.println(Arrays.toString(backingArray));//[Goodbye, Simulation]
dan1st | Daniel
Similar to List.of, List.copyOf creates an immutable List that is a copy of another List Since this is a copy, changing the original List doesn't modify the copy.
List<String> originalList = new ArrayList<>();
originalList.add("Hello");
originalList.add("World");

List<String> immutableCopy = List.copyOf(originalList);
System.out.println(immutableCopy.get(0));//Hello
//immutableCopy.remove(0);//UnsupportedOperationException
//immutableCopy.set(0, "Goodbye");//UnsupportedOperationException
//immutableCopy.add("return");//UnsupportedOperationException
originalList.set(1,"Java");
System.out.println(originalList);//[Hello, Java]
System.out.println(immutableCopy);//[Hello, World]
List<String> originalList = new ArrayList<>();
originalList.add("Hello");
originalList.add("World");

List<String> immutableCopy = List.copyOf(originalList);
System.out.println(immutableCopy.get(0));//Hello
//immutableCopy.remove(0);//UnsupportedOperationException
//immutableCopy.set(0, "Goodbye");//UnsupportedOperationException
//immutableCopy.add("return");//UnsupportedOperationException
originalList.set(1,"Java");
System.out.println(originalList);//[Hello, Java]
System.out.println(immutableCopy);//[Hello, World]
On the other hand, Collections.unmodifiableList creates an unmodifiable view on a List. While that view cannot be modified, changes to the backing List are propagated to the view:
List<String> backingList = new ArrayList<>();
backingList.add("Hello");
backingList.add("World");

List<String> unmodifiableView = Collections.unmodifiableList(backingList);
System.out.println(unmodifiableView.get(0));//Hello
//unmodifiableView.remove(0);//UnsupportedOperationException
//unmodifiableView.set(0, "Goodbye");//UnsupportedOperationException
//unmodifiableView.add("return");//UnsupportedOperationException
backingList.set(1,"Java");
System.out.println(unmodifiableView);//[Hello, Java]
System.out.println(backingList);//[Hello, Java]
List<String> backingList = new ArrayList<>();
backingList.add("Hello");
backingList.add("World");

List<String> unmodifiableView = Collections.unmodifiableList(backingList);
System.out.println(unmodifiableView.get(0));//Hello
//unmodifiableView.remove(0);//UnsupportedOperationException
//unmodifiableView.set(0, "Goodbye");//UnsupportedOperationException
//unmodifiableView.add("return");//UnsupportedOperationException
backingList.set(1,"Java");
System.out.println(unmodifiableView);//[Hello, Java]
System.out.println(backingList);//[Hello, Java]
📖 Sample answer from dan1st

Did you find this page helpful?