Java Reference ยท Collections
Java Collections.unmodifiableList() method
Reference for Collections.unmodifiableList() and its siblings: read-only views over a collection, how they differ from copies and List.copyOf.
Collections.unmodifiableList() returns a read-only view of a list. Calls that would change it (add, remove, set, sort, clear, iterator remove) throw UnsupportedOperationException. Reading works as usual.
The key word is view. The wrapper does not copy the data: if someone still holds the original list and changes it, the change appears in the view. There are matching methods for Collection, Set, SortedSet, Map, SortedMap, NavigableSet and NavigableMap.
List<String> view = Collections.unmodifiableList(items);
Try it
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.
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
public static <T> List<T> unmodifiableList(List<? extends T> list) public static <T> Set<T> unmodifiableSet(Set<? extends T> s) public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
| Name | Description |
|---|---|
list, s, m, c |
The backing collection whose contents are exposed. |
Returns: a read-only view with the same iteration order as the source.
Throws: NullPointerException if the argument is null. Mutator calls on the result throw UnsupportedOperationException.
Usage notes
- Shallow: only the collection is protected. Mutable elements inside, such as
StringBuilderor your own mutable objects, can still be changed. - Live view: if the owner keeps changing the source, readers see it. This is useful for exposing internal state cheaply, but it is not a safe snapshot and is not thread-safe.
- For a snapshot, use
List.copyOf(source),Set.copyOforMap.copyOf(Java 10+). They copy once and rejectnullelements. If the argument is already an immutable collection created by these methods, they return it unchanged. List.of(...)is immutable from the start and should be your default for literal lists.- Not a type-level guarantee: the view is still a
List, so the compiler does not warn aboutadd. Mistakes show up only at run time. - Performance: every call delegates to the backing collection, a very small cost. A wrapped
ArrayListkeeps fastget(i). - Equality:
equalsandhashCodeof a list or set view delegate to the source, so a view equals a list with the same elements.unmodifiableCollectiondoes not, and falls back to identity. - Sequenced collections (Java 21):
unmodifiableSequencedCollection,unmodifiableSequencedSetandunmodifiableSequencedMapexist as well.