Home Java Collections Methods unmodifiableMap() Java Collections unmodifiableMap() Method Definition In Java, Collections.unmodifiableMap(m) returns a read-only view of a map. You can call get, getOrDefault, containsKey, and loop over entries, but anything that would add, remove, or replace a mapping throws UnsupportedOperationException.
The protection is shallow : the map's structure is locked, but if a value is itself a mutable object (such as a List), that object can still be changed.
A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.unmodifiableMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Wrap the map you want to share.
Map<String, String> settings = new TreeMap<>();
settings.put("theme", "dark");
Map<String, String> config = Collections.unmodifiableMap(settings); Call What it does unmodifiableMap(m)Read-only live view. view.getOrDefault(k, d)Allowed: it only reads. view.put / merge / compute...All throw UnsupportedOperationException. Map.copyOf(m)Java 10+ immutable copy; rejects null keys and values.
Available since Java 1.2. Its keySet(), values(), and entrySet() are read-only too, and entries from entrySet() reject setValue.
Library declarations // java.util.Collections
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) Parameters Parameter Description mThe map to expose read-only. Must not be null.
Return Value Return Value Description Map<K,V>A read-only view of m.
Important
put, remove, clear, putAll, Java 8 defaults like merge, compute, and putIfAbsent, and Map.Entry.setValue all throw UnsupportedOperationException. Mutable values stored in the map are not protected.
Compare with Similar Methods Related maps:
Java Examples 1. Read settings 2. Writes fail 3. Shallow protection Example 1 — Read Configuration Through a Read-Only Map Look up existing and missing settings through the view.
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class CollectionsUnmodifiableMapConfig {
static Map<String, String> load() {
Map<String, String> settings = new TreeMap<>();
settings.put("theme", "dark");
settings.put("lang", "en");
return Collections.unmodifiableMap(settings);
}
public static void main(String[] args) {
Map<String, String> config = load();
System.out.println(config);
System.out.println("theme = " + config.get("theme"));
System.out.println("font = " + config.getOrDefault("font", "Arial"));
System.out.println("has lang? " + config.containsKey("lang"));
}
} {lang=en, theme=dark}
theme = dark
font = Arial
has lang? true How It Works Wrapped on the way out. load fills a TreeMap and returns only the read-only view, so the original map is unreachable.Normal reads. get("theme") returns "dark", and the map prints in key order.Read-only helpers work. getOrDefault supplies "Arial" for the missing font, and containsKey confirms "lang" exists.Example 2 — put, merge, and setValue Are All Blocked Try three different ways to change a stock count.
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class CollectionsUnmodifiableMapBlocked {
public static void main(String[] args) {
Map<String, Integer> stock = new TreeMap<>();
stock.put("apple", 5);
Map<String, Integer> view = Collections.unmodifiableMap(stock);
try {
view.put("pear", 2);
} catch (UnsupportedOperationException e) {
System.out.println("put: " + e.getClass().getSimpleName());
}
try {
view.merge("apple", 1, Integer::sum);
} catch (UnsupportedOperationException e) {
System.out.println("merge: " + e.getClass().getSimpleName());
}
for (Map.Entry<String, Integer> e : view.entrySet()) {
try {
e.setValue(0);
} catch (UnsupportedOperationException ex) {
System.out.println("entry.setValue: " + ex.getClass().getSimpleName());
}
}
System.out.println("still " + view);
}
} put: UnsupportedOperationException
merge: UnsupportedOperationException
entry.setValue: UnsupportedOperationException
still {apple=5} How It Works Classic write. put of a new key is refused.Java 8 defaults too. merge would add 1 to apple's count; the view overrides it to throw instead.Entries are wrapped. Each entry from entrySet() refuses setValue, so the count stays at 5.Example 3 — Mutable Values Can Still Change A read-only map of teams still lets you add players to a team list. Wrap the values too for deeper protection.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class CollectionsUnmodifiableMapShallow {
public static void main(String[] args) {
Map<String, List<String>> teams = new TreeMap<>();
teams.put("red", new ArrayList<>(Arrays.asList("ana")));
Map<String, List<String>> view = Collections.unmodifiableMap(teams);
view.get("red").add("ben");
System.out.println("through the view: " + view);
Map<String, List<String>> inner = new TreeMap<>();
for (Map.Entry<String, List<String>> e : teams.entrySet()) {
inner.put(e.getKey(), Collections.unmodifiableList(e.getValue()));
}
Map<String, List<String>> deep = Collections.unmodifiableMap(inner);
try {
deep.get("red").add("cy");
} catch (UnsupportedOperationException e) {
System.out.println("values wrapped too: " + e.getClass().getSimpleName());
}
}
} through the view: {red=[ana, ben]}
values wrapped too: UnsupportedOperationException How It Works Locked keys, open values. view.get("red") returns the real ArrayList, and adding "ben" to it is allowed.Wrap each value. The second map stores unmodifiableList views as its values before being wrapped itself.Two layers. Now both the map and each team list refuse changes, so adding "cy" throws.Frequently Asked Questions What does Collections.unmodifiableMap() do? It returns a read-only view of a Map. Lookups work; put, remove, merge, and similar methods throw UnsupportedOperationException.
Is an unmodifiable map deeply immutable? No. Only the mappings are locked. Mutable values, such as lists stored in the map, can still be modified.
Does getOrDefault() work on an unmodifiable map? Yes. It only reads, so it behaves like on the original map.
Can the unmodifiable map change? Yes, if someone modifies the original map. The view reflects those changes.
unmodifiableMap() or Map.copyOf()? Map.copyOf (Java 10+) makes an immutable copy and rejects nulls; unmodifiableMap is a live view and works on any Java version.
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.
7 people found this page helpful
Helpful Share Copy link Suggestion