Arrays.asList vs List.of

Naveen AnamaneniUpdated · 4 min read

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.

AsListVsOf.java▶ Try it
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));
    }
}
Output
asList set: ok
asList add: UnsupportedOperationException
listOf set: UnsupportedOperationException
listOf add: UnsupportedOperationException
asList null: ok
listOf null: NullPointerException
listOf contains(null): NullPointerException

Explanation: 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:

SharedArray.java▶ Try it
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));
    }
}
Output
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.asListList.of
Introduced inJava 1.2Java 9
add / removeNot allowed (fixed size)Not allowed
setAllowedNot allowed
null elementsAllowedNot allowed (NullPointerException)
Link to an arrayA view: shares the arrayA copy: independent
contains(null)Returns falseThrows NullPointerException
With an int[]Gives List<int[]> with one elementGives 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

  1. Easy: Convert Arrays.asList("a", "b") into a list you can add "c" to, and print it.
  2. Harder: Show what Arrays.asList(new int[] {1, 2, 3}).size() returns, explain why, and write a correct way to get a List<Integer> from an int[].

Quick check: Arrays.asList vs List.of

4 questions
1Which list allows set()?
2Which list allows null elements?
3Changing the original array changes which list?
4Can you add to either list?

Key Takeaways

Was this tutorial helpful?

Lesson 295 of 482