Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java List.of() method

Reference for Java List.of() and List.copyOf(): create immutable lists, null rules, the Arrays.asList() difference and common exceptions.

List.of() is a static factory method (since Java 9) that creates an immutable list from the elements you give it. It is the shortest way to write a fixed list of constants. Attempts to add, remove or replace elements throw UnsupportedOperationException.

The returned list also rejects null elements. Together with List.copyOf() (Java 10), which creates an immutable copy of an existing collection, it is the standard tool for lists that should not change after creation.

Java
List<String> days = List.of("Mon", "Tue", "Wed");
List<Integer> none = List.of();

Try it

Java Creating immutable lists

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java of() versus copyOf() and asList()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static <E> List<E> of()
static <E> List<E> of(E e1)
static <E> List<E> of(E e1, E e2)        // ... up to ten fixed-arity overloads
static <E> List<E> of(E... elements)
static <E> List<E> copyOf(Collection<? extends E> coll)
Name Description
e1, e2, ... / elements The elements, in order. Duplicates are allowed.
coll (copyOf) The collection to copy. Its elements must not be null.

Returns: an unmodifiable List. copyOf() returns the argument itself if it is already an immutable list created by these factories.

Throws: NullPointerException if any element (or the collection) is null; UnsupportedOperationException from every mutator, including sort(), removeIf() and Collections.sort().

Usage notes

  • Truly immutable, not just a view: a list from List.of() does not change when other variables change, unlike Collections.unmodifiableList(), which is a read-only view of a mutable list.
  • Shallow: the elements themselves can still be mutable objects.
  • No nulls: contains(null) and indexOf(null) also throw NullPointerException on these lists.
  • Do not rely on the concrete class or its toString() beyond the usual bracket form.
  • Arrays: passing an Integer[] spreads it into elements; passing an int[] creates a one-element list of the array.
  • Compared with Arrays.asList(): that one allows null, set() and writes through to the array; List.of() allows none of these.
  • Memory: small lists use compact internal classes, so List.of() is a good fit for constants.
  • Java version: of is Java 9; copyOf is Java 10. In Java 8 use Collections.unmodifiableList(Arrays.asList(...)).