In Java, Collections.unmodifiableSet(s) returns a read-only view of a set. Lookups such as contains work, while add, remove, retainAll, and the rest throw UnsupportedOperationException.
A classic use is a static final constant of allowed values: build the set once, wrap it, and no code anywhere can quietly add to 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.unmodifiableSet(...), after import java.util.Collections;. You never create a Collections object.
Syntax
Wrap a freshly built set to create a constant.
Java
static final Set<String> IMAGE_TYPES =
Collections.unmodifiableSet(new TreeSet<>(Arrays.asList("gif", "jpg", "png")));
Call
What it does
unmodifiableSet(s)
Read-only view; iteration order follows s.
view.contains(x)
Fast membership test, same as on s.
new TreeSet<>(view)
Changeable copy for set algebra.
Set.of(...)
Java 9+ immutable set; no null, no duplicates allowed in the call.
Available since Java 1.2. Unlike unmodifiableCollection, this view uses set equality, so it equals any set with the same elements.
Library declarations
Java
// java.util.Collections
public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
In-place operation.retainAll would delete "go" from mine, so the view throws.
Copy first. A new TreeSet built from the view is free to shrink.
Result. The copy keeps the two shared languages, and the read-only set still has all three.
Example 3 — The View Equals Any Set with the Same Elements
Compare a read-only TreeSet view with a LinkedHashSet that holds the same numbers in a different order.
Java
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
public class CollectionsUnmodifiableSetEquality {
public static void main(String[] args) {
Set<Integer> a = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(3, 1, 2)));
Set<Integer> b = new LinkedHashSet<>(Arrays.asList(3, 1, 2));
System.out.println("a = " + a + ", b = " + b);
System.out.println("a.equals(b): " + a.equals(b) + ", b.equals(a): " + b.equals(a));
System.out.println("a.containsAll(b): " + a.containsAll(b));
}
}
Output
a = [1, 2, 3], b = [3, 1, 2]
a.equals(b): true, b.equals(a): true
a.containsAll(b): true
How It Works
Different order. The view prints in the TreeSet's sorted order; the LinkedHashSet keeps insertion order 3, 1, 2.
Set contract. Sets are equal when they hold the same elements, whatever their class or order, and the wrapper keeps that rule in both directions.
Contrast. An unmodifiableCollection view would answer false here, because it compares by identity; containsAll confirms the elements match.
Frequently Asked Questions
It returns a read-only view of a Set. contains and iteration work; changes throw UnsupportedOperationException.
static final Set<T> X = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(...))); or Set.of(...) in Java 9+.
Yes, if code still holds the original set and modifies it. Wrap a set that nothing else references.
Yes, when they contain the same elements. The view follows the Set.equals contract.
Set.of (Java 9+) is a compact immutable set that rejects null and duplicates in the call; unmodifiableSet wraps any set you choose, on any Java version.