Java 25 (LTS) reference · List and ArrayList Methods

Java List.of() Method

Create an immutable list from values. Adding or removing later throws UnsupportedOperationException.

Java 25 (LTS) Since Java 9

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

List.of(e1, e2, ...)

Parameters

ParameterDescription
elements
E...
The values (no nulls).

Return value

An unmodifiable List<E>.

Examples

An immutable list
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> days = List.of("Mon", "Tue", "Wed");
        System.out.println(days + " size " + days.size());
        try {
            days.add("Thu");
        } catch (UnsupportedOperationException e) {
            System.out.println("List.of lists are immutable");
        }
    }
}

Output

[Mon, Tue, Wed] size 3
List.of lists are immutable

Run this on your computer — see “Get Started” for setup.

Make a mutable copy
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> fixed = List.of(3, 1, 2);
        List<Integer> copy = new ArrayList<>(fixed);  // a mutable copy
        copy.add(4);
        System.out.println(copy);
    }
}

Output

[3, 1, 2, 4]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 9Added

Was this page helpful?