Home Java Collections Methods unmodifiableList() Java Collections unmodifiableList() Method 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.
private final List<String> songs = new ArrayList<>();
public List<String> getSongs() {
return Collections.unmodifiableList(songs);
} Call What 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.util.Collections
public static <T> List<T> unmodifiableList(List<? extends T> list) Parameters Parameter Description listThe list to expose read-only. Must not be null.
Return Value Return Value Description List<T>A read-only view of list.
Important
Any structural or positional change throws UnsupportedOperationException, including set, sort, replaceAll, and helpers such as Collections.sort(view) or Collections.reverse(view). The original list can still change, and the view reflects that.
Compare with Similar Methods Related read-only lists:
Java Examples 1. Safe getter 2. Sorting fails 3. View vs snapshot Example 1 — A Playlist Getter Callers Cannot Edit Read songs by position through the view; changing a track or adding one fails.
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());
}
}
} track 2: Rise, Outro at index 2
set: UnsupportedOperationException
add: UnsupportedOperationException How It Works Positional reads. get(1) and indexOf work exactly as on the ArrayList underneath.No replacing. set(0, "Remix") would change a track, so the view throws.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.
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);
}
} sort view: UnsupportedOperationException
reverse view: UnsupportedOperationException
sorted copy [1, 2, 3], view still [3, 1, 2] How It Works sort writes. Collections.sort calls List.sort, which the view refuses.reverse writes too. Collections.reverse swaps elements with set, so it also throws.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.
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);
}
} view: [v1, v2]
snapshot: [v1] How It Works Two wrappers. view wraps live itself; snapshot wraps a private copy taken at that moment.Owner edits. Adding "v2" to live shows up in view, because it reads straight through.Frozen. Nobody holds the copied list, so snapshot stays [v1] forever.Frequently Asked Questions What does Collections.unmodifiableList() do? It returns a read-only view of a List. Reads work; add, set, remove, and sort throw UnsupportedOperationException.
Does unmodifiableList() make the list immutable? Not by itself. The original list can still change, and the view shows those changes. Wrap a copy for a frozen list.
Why does Collections.sort() fail on my list? The list is probably an unmodifiable view. Copy it into a new ArrayList and sort the copy.
unmodifiableList() or List.copyOf()? List.copyOf (Java 10+) makes an immutable copy and rejects null elements; unmodifiableList is a live view and works on any Java version.
Is the view equal to the original list? Yes. It follows List.equals, so it is equal to any list with the same elements in the same order.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion