How to Initialize ArrayList in Java with List.of() & Arrays.asList()

Introduction:

  • List.of(...) (Java 9+) creates an unmodifiable list (no add/remove/set), and disallows null elements.
  • Arrays.asList(...) creates a fixed-size list backed by an array (supports set, but add/remove throw UnsupportedOperationException).
  • If you want a modifiable ArrayList, wrap either of these in new ArrayList<>(...) to copy elements into a regular, resizable ArrayList.

Initializing an ArrayList with Multiple Items

To initialize an ArrayList with multiple items in a single line, you can create a List of items using either Arrays.asList() or List.of() methods. Both methods return a list containing the items passed to the factory method.

In the following examples, we add two strings "A" and "B" to the ArrayList:

1. Using Arrays.asList()

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FromArraysAsList {
    public static void main(String[] args) {
        // Arrays.asList returns a fixed-size list backed by the provided array
        List<String> fixed = Arrays.asList("A", "B");

        // fixed.set(0, "X") is allowed (changes the backing array)
        fixed.set(0, "X");
        System.out.println(fixed); // [X, B]

        // fixed.add("D")  <-- throws UnsupportedOperationException

        // To get a fully modifiable ArrayList, copy it:
        ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B"));
        list.add("C");            // OK
        System.out.println(list); // [A, B, C]
    }
}

✔ Allows null values.
✔ Works in all Java versions (since Java 1.2).

Arrays.asList(new int[]{1,2,3}) produces a List<int[]> of size 1 (because the primitive array is treated as a single element). Use boxed types (Integer[]) or streams (IntStream) to avoid this.

2. Using List.of() (Java 9+)

import java.util.ArrayList;
import java.util.List;

public class FromListOf {
    public static void main(String[] args) {
        // List.of returns an unmodifiable List
        List<Integer> list= List.of("A", "B"); // Java 9+

        // Create a mutable ArrayList by copying
        ArrayList<Integer> mutable = new ArrayList<>(list);

        System.out.println(mutable); // ["A", "B"]
        mutable.add("C");              // works because `mutable` is an ArrayList
        System.out.println(mutable); // ["A", "B","C"]
    }
}

✔ More concise syntax.
❌ List.of(“A”, “B”, null) Does not allow null values (throws NullPointerException).
List.of(...) list itself is unmodifiable — calling list.add("C") would throw UnsupportedOperationException.


Best practice (short)

  • Use List.of(...) for concise, immutable lists (configuration, constants) — Java 9+.
  • Use Arrays.asList(...) when you want a quick fixed-size list or when you already have an array and want a list view.
  • If you want a mutable ArrayList, always wrap/copy:
    • ArrayList<T> mutable = new ArrayList<>(List.of(…)); // recommended (Java 9+)
      ArrayList<T> mutable2 = new ArrayList<>(Arrays.asList(…));

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *