Sub topic 11
Read-Only Sequenced Wrappers
This chapter assumes that you know List, Set, Map, Deque, interfaces, generics, and the difference between ordered and sorted collections. The examples require Java 21 or newer.
A sequenced collection has a defined encounter order. Java 21 provides read-only wrappers for sequenced collections, sets, and maps:
Collections.unmodifiableSequencedCollection(...)Collections.unmodifiableSequencedSet(...)Collections.unmodifiableSequencedMap(...)
Each wrapper is an unmodifiable view. A view is an object that presents another object through a different interface or policy. The wrapper blocks changes made through the view, but it does not copy the data.
Creating an unmodifiable view
The following example wraps a LinkedHashSet:
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.SequencedSet;
public class ReadOnlySet {
public static void main(String[] args) {
LinkedHashSet<String> mutable =
new LinkedHashSet<>(List.of("red", "green", "blue"));
SequencedSet<String> readOnly =
Collections.unmodifiableSequencedSet(mutable);
System.out.println(readOnly); // [red, green, blue]
System.out.println(readOnly.getFirst()); // red
System.out.println(readOnly.reversed()); // [blue, green, red]
try {
readOnly.addFirst("yellow");
} catch (UnsupportedOperationException e) {
System.out.println("The view cannot be changed");
}
mutable.addFirst("yellow");
System.out.println(readOnly); // [yellow, red, green, blue]
}
}The call to addFirst fails because the wrapper blocks mutating operations. The later change through mutable succeeds, and the wrapper immediately reflects it. This is the main difference between an unmodifiable view and an independent immutable copy.
The wrapper also protects operations exposed through derived views. For example, readOnly.reversed() is itself read-only. Calling addFirst on that reversed view also throws UnsupportedOperationException.
Sequenced maps and their views
Maps have a separate wrapper because map operations differ from collection operations.
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.SequencedMap;
public class ReadOnlyMap {
public static void main(String[] args) {
LinkedHashMap<String, Integer> mutable = new LinkedHashMap<>();
mutable.put("one", 1);
mutable.put("two", 2);
mutable.put("three", 3);
SequencedMap<String, Integer> readOnly =
Collections.unmodifiableSequencedMap(mutable);
System.out.println(readOnly.firstEntry()); // one=1
System.out.println(readOnly.lastEntry()); // three=3
System.out.println(readOnly.reversed()); // {three=3, two=2, one=1}
System.out.println(readOnly.sequencedKeySet()); // [one, two, three]
try {
readOnly.pollLastEntry();
} catch (UnsupportedOperationException e) {
System.out.println("Removing through the view is blocked");
}
try {
readOnly.putFirst("zero", 0);
} catch (UnsupportedOperationException e) {
System.out.println("Insertion through the view is blocked");
}
}
}The map wrapper also supplies read-only sequenced views:
sequencedKeySet()exposes keys in encounter order.sequencedValues()exposes values in encounter order.sequencedEntrySet()exposes entries in encounter order.
Changes through these views are blocked. For example, removing a key from sequencedKeySet() throws UnsupportedOperationException. The entries returned by the entry view do not allow setValue to modify the map.
| Wrapped type | Wrapper method | Main protected operations |
|---|---|---|
| `SequencedCollection` | `unmodifiableSequencedCollection` | add, remove, replace, reorder |
| `SequencedSet` | `unmodifiableSequencedSet` | addFirst, addLast, removeFirst, removeLast |
| `SequencedMap` | `unmodifiableSequencedMap` | putFirst, putLast, pollFirstEntry, pollLastEntry |
View relationships
The wrapper, its reversed view, and its map views all refer to the same underlying data.
Use a wrapper when an API should allow callers to inspect encounter order without changing the collection. Keep the mutable collection private and return the sequenced interface:
static SequencedSet<String> colors() {
LinkedHashSet<String> values =
new LinkedHashSet<>(java.util.List.of("red", "green", "blue"));
return java.util.Collections.unmodifiableSequencedSet(values);
}If the caller needs a snapshot that cannot change when the original changes, create a separate copy before wrapping it. An unmodifiable wrapper alone does not provide snapshot behavior.
Exercise
What happens when an underlying LinkedHashMap is changed after it has been passed to unmodifiableSequencedMap?
Show answer
The unmodifiable map view reflects the change, but callers still cannot modify the map through that view.