Java Collections unmodifiableList() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.unmodifiableList(list) returns a read-only view of a list. get, indexOf, size, and iteration work as usual, while add, set, remove, and sort throw UnsupportedOperationException.

The common use is a getter that returns an object's internal list: callers can read every element, but only the owning class can change it.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.unmodifiableList(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Wrap the list in the getter.

Java
private final List<String> songs = new ArrayList<>();

public List<String> getSongs() {
    return Collections.unmodifiableList(songs);
}
CallWhat it does
unmodifiableList(list)Read-only live view.
unmodifiableList(new ArrayList<>(list))Read-only snapshot that never changes.
List.copyOf(list)Java 10+ immutable copy; rejects null elements.
new ArrayList<>(view)Changeable copy for sorting or editing.

Available since Java 1.2. If the original list supports fast index access (RandomAccess, like ArrayList), the view does too.

Library declarations
Java
// java.util.Collections
public static <T> List<T> unmodifiableList(List<? extends T> list)

Parameters

ParameterDescription
listThe list to expose read-only. Must not be null.

Return Value

Return ValueDescription
List<T>A read-only view of list.

Compare with Similar Methods

Related read-only lists:

MethodHow it differs
unmodifiableCollection(c)General collection; no index access, identity equals.
emptyList()Immutable list with nothing in it.
checkedList(l, type)Changeable, but rejects wrong element types.

Java Examples

Example 1 — A Playlist Getter Callers Cannot Edit

Read songs by position through the view; changing a track or adding one fails.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsUnmodifiableListPlaylist {
    private final List<String> songs = new ArrayList<>();

    List<String> getSongs() {
        return Collections.unmodifiableList(songs);
    }

    public static void main(String[] args) {
        CollectionsUnmodifiableListPlaylist p = new CollectionsUnmodifiableListPlaylist();
        p.songs.add("Intro");
        p.songs.add("Rise");
        p.songs.add("Outro");

        List<String> view = p.getSongs();
        System.out.println("track 2: " + view.get(1) + ", Outro at index " + view.indexOf("Outro"));
        try {
            view.set(0, "Remix");
        } catch (UnsupportedOperationException e) {
            System.out.println("set: " + e.getClass().getSimpleName());
        }
        try {
            view.add("Bonus");
        } catch (UnsupportedOperationException e) {
            System.out.println("add: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Positional reads. get(1) and indexOf work exactly as on the ArrayList underneath.
  2. No replacing. set(0, "Remix") would change a track, so the view throws.
  3. No growing. add("Bonus") fails the same way; only the playlist class can edit songs.

Example 2 — Sort or Reverse a Copy, Not the View

Utility methods that rearrange elements cannot work on a read-only list.

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

public class CollectionsUnmodifiableListSort {
    public static void main(String[] args) {
        List<Integer> view = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(3, 1, 2)));
        try {
            Collections.sort(view);
        } catch (UnsupportedOperationException e) {
            System.out.println("sort view: " + e.getClass().getSimpleName());
        }
        try {
            Collections.reverse(view);
        } catch (UnsupportedOperationException e) {
            System.out.println("reverse view: " + e.getClass().getSimpleName());
        }

        List<Integer> sorted = new ArrayList<>(view);
        Collections.sort(sorted);
        System.out.println("sorted copy " + sorted + ", view still " + view);
    }
}

How It Works

  1. sort writes. Collections.sort calls List.sort, which the view refuses.
  2. reverse writes too. Collections.reverse swaps elements with set, so it also throws.
  3. Copy, then rearrange. Copying into a new ArrayList gives a list you may sort; the view keeps its original order.

Example 3 — Live View or Frozen Snapshot?

Wrap the list directly, and wrap a copy of it, then let the owner add a version.

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

public class CollectionsUnmodifiableListSnapshot {
    public static void main(String[] args) {
        List<String> live = new ArrayList<>(Arrays.asList("v1"));
        List<String> view = Collections.unmodifiableList(live);
        List<String> snapshot = Collections.unmodifiableList(new ArrayList<>(live));

        live.add("v2");
        System.out.println("view:     " + view);
        System.out.println("snapshot: " + snapshot);
    }
}

How It Works

  1. Two wrappers. view wraps live itself; snapshot wraps a private copy taken at that moment.
  2. Owner edits. Adding "v2" to live shows up in view, because it reads straight through.
  3. Frozen. Nobody holds the copied list, so snapshot stays [v1] forever.

Frequently Asked Questions

It returns a read-only view of a List. Reads work; add, set, remove, and sort throw UnsupportedOperationException.
Not by itself. The original list can still change, and the view shows those changes. Wrap a copy for a frozen list.
The list is probably an unmodifiable view. Copy it into a new ArrayList and sort the copy.
List.copyOf (Java 10+) makes an immutable copy and rejects null elements; unmodifiableList is a live view and works on any Java version.
Yes. It follows List.equals, so it is equal to any list with the same elements in the same order.

Next: unmodifiableMap()

Wrap a Map in a read-only view.

unmodifiableMap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful