Skip to main content

How to Create a List in Java with List.of

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

List.of("editor", "viewer") creates a Java list of the values you give it, in that order, in one line. The list can't be changed afterwards: adding, removing or replacing an element throws an UnsupportedOperationException.

Syntax​

Syntax
List<Type> name = List.of(element1, element2, ...);
PartWhat it does
List.of(...)Creates an unmodifiable list of the given elements, in the order given.
element1, element2, ...The elements: none, one or as many as you need. None of them can be null.
TypeThe type of the elements, such as String.
nameThe variable's name.

The List documentation: "The List.of and List.copyOf static factory methods provide a convenient way to create unmodifiable lists." It goes on: "They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown."

The Example​

The example makes a list of two roles and prints the list, its second element and its size. A changed version then tries to add a third role.

Prerequisites​

  • A JDK 11 or later, which can run a single .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which creates a list of the roles editor and viewer with List.of, then prints the list, the element at position 1 and the list's size.

Main.java
import java.util.List;

public class Main {

public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");

System.out.println(roles);
System.out.println(roles.get(1));
System.out.println(roles.size());
}
}

Step 2: Run It​

Terminal
java Main.java
Output
[editor, viewer]
viewer
2

java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.

Step 3: Try to Add an Element​

Replace Main.java with a version that adds admin to the list:

Main.java
import java.util.List;

public class Main {

public static void main(String[] args) {
List<String> roles = List.of("editor", "viewer");

roles.add("admin");
System.out.println(roles);
}
}
Terminal
java Main.java
Output
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:159)
at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:164)
at Main.main(Main.java:8)

A zsh terminal where java Main.java prints the list editor, viewer in square brackets, then viewer and 2, and then, for the changed file, stops with java.lang.UnsupportedOperationException thrown from ImmutableCollections.uoe.

What Happened​

  • System.out.println(roles) printed [editor, viewer], in the order the elements were given.
  • roles.get(1) returned viewer. Positions start at 0, so 1 is the second element.
  • roles.size() returned 2.
  • roles.add("admin") threw UnsupportedOperationException. The variable is a plain List, which has an add method, so the code compiled. The list itself refused the change when the program ran.

Common Mistakes​

MistakeWhat happensDo this instead
Adding, removing or replacing an element of a List.of listUnsupportedOperationException, from set as well as addCopy it into a list that can change: new ArrayList<>(List.of("editor", "viewer"))
A null among the elementsNullPointerException, thrown from Objects.requireNonNull inside List.ofLeave null out
get(2) on a list of two elementsIndexOutOfBoundsException: Index: 2 Size: 2Use positions from 0 to size() - 1
Expecting Arrays.asList to behave the sameIt lets set replace an element, but add still throws UnsupportedOperationExceptionUse List.of for a list that never changes, and an ArrayList for one that does

Troubleshooting​

SymptomLikely causeFix
java.lang.UnsupportedOperationException at java.util.ImmutableCollections.uoeThe code changes a list made with List.ofCopy it into an ArrayList first
java.lang.NullPointerException at java.util.Objects.requireNonNull, with java.util.List.of further down the stack traceOne of the elements is nullLeave the null out, or check the values first
java.lang.IndexOutOfBoundsException: Index: 2 Size: 2The position is past the last elementCheck it against size()

FAQ​

Can a List.of list be empty?​

Yes. List.of() with nothing between the parentheses makes an empty list, whose size() is 0.

What's the difference between List.of and an ArrayList?​

A list from List.of never changes and never holds null. An ArrayList grows and shrinks with add and remove. new ArrayList<>(List.of(...)) starts an ArrayList with the same elements, and adding admin to one printed [editor, viewer, admin].

Can the elements of a List.of list change?​

The list can't, but an element that is itself changeable can. The documentation: "However, if the contained elements are themselves mutable, this may cause the List's contents to appear to change." Strings never change, so a list of strings stays exactly as it was made.

Conclusion​

List.of makes a list in one line, with its elements in the order you give them. The list can't change and can't hold null, so copy it into an ArrayList when the code needs to change it.

Additional Resources​