Arrays.asList vs List.of
Arrays.asList(...) and List.of(...) are two quick ways to create a list from a few values in one line. They look alike, but they behave very differently. Arrays.asList returns a fixed-size list that is a view of an array: you can change elements with set, but you cannot add or remove. List.of, added in Java 9, returns a fully unmodifiable list that doesn't accept null. In this lesson, you will learn all the differences, shown in code.
Example: What Each List Allows
Let's see an example that tries the same operations on both lists.
import java.util.Arrays;
import java.util.List;
public class AsListVsOf {
static void tryIt(String label, Runnable action) {
try {
action.run();
System.out.println(label + ": ok");
} catch (RuntimeException e) {
System.out.println(label + ": " + e.getClass().getSimpleName());
}
}
public static void main(String[] args) {
List<String> asList = Arrays.asList("Navika", "Srihitha");
List<String> listOf = List.of("Navika", "Srihitha");
tryIt("asList set", () -> asList.set(0, "Advitha"));
tryIt("asList add", () -> asList.add("Advitha"));
tryIt("listOf set", () -> listOf.set(0, "Advitha"));
tryIt("listOf add", () -> listOf.add("Advitha"));
tryIt("asList null", () -> Arrays.asList("Navika", null));
tryIt("listOf null", () -> List.of("Navika", null));
tryIt("listOf contains(null)", () -> listOf.contains(null));
}
}asList set: ok
asList add: UnsupportedOperationException
listOf set: UnsupportedOperationException
listOf add: UnsupportedOperationException
asList null: ok
listOf null: NullPointerException
listOf contains(null): NullPointerExceptionExplanation: Arrays.asList allows set but not add, because its size is fixed to the array's length. List.of allows neither. Arrays.asList accepts null elements; List.of rejects them, and even contains(null) throws.
Example: Arrays.asList Shares the Array
Arrays.asList doesn't copy the array. The list and the array are the same data:
import java.util.Arrays;
import java.util.List;
public class SharedArray {
public static void main(String[] args) {
String[] names = {"Navika", "Srihitha", "Advitha"};
List<String> view = Arrays.asList(names);
List<String> copy = List.of(names);
names[0] = "Anamaneni";
System.out.println("asList: " + view);
System.out.println("List.of: " + copy);
view.set(1, "SRIHITHA");
System.out.println("array: " + Arrays.toString(names));
}
}asList: [Anamaneni, Srihitha, Advitha]
List.of: [Navika, Srihitha, Advitha]
array: [Anamaneni, SRIHITHA, Advitha]Explanation: Changing the array also changed the Arrays.asList view, and changing the view with set changed the array. List.of(names) copied the elements, so it kept the original values.
Differences Between Arrays.asList and List.of
Arrays.asList | List.of | |
|---|---|---|
| Introduced in | Java 1.2 | Java 9 |
add / remove | Not allowed (fixed size) | Not allowed |
set | Allowed | Not allowed |
null elements | Allowed | Not allowed (NullPointerException) |
| Link to an array | A view: shares the array | A copy: independent |
contains(null) | Returns false | Throws NullPointerException |
With an int[] | Gives List<int[]> with one element | Gives List<int[]> with one element |
Common Mistakes
Calling add on Arrays.asList(...). It throws UnsupportedOperationException; copy it into an ArrayList first.
Passing an int[] to Arrays.asList. You get a list containing one array, not a list of numbers; use Integer[] or IntStream.
Calling contains(null) on a List.of list. It throws instead of returning false.
Interview Corner
What is the difference between Arrays.asList() and List.of()?
Arrays.asList returns a fixed-size list backed by the given array: set works and writes through to the array, changes to the array show in the list, null elements are allowed, but add and remove throw. List.of (Java 9) returns an unmodifiable list that copies its elements: every change operation throws, and null elements, and even contains(null), throw NullPointerException.
Practice
- Easy: Convert
Arrays.asList("a", "b")into a list you can add "c" to, and print it. - Harder: Show what
Arrays.asList(new int[] {1, 2, 3}).size()returns, explain why, and write a correct way to get aList<Integer>from anint[].
Key Takeaways
Was this tutorial helpful?