Java Collections unmodifiableMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

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.

Java
Map<String, String> settings = new TreeMap<>();
settings.put("theme", "dark");
Map<String, String> config = Collections.unmodifiableMap(settings);
CallWhat 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
// java.util.Collections
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m)

Parameters

ParameterDescription
mThe map to expose read-only. Must not be null.

Return Value

Return ValueDescription
Map<K,V>A read-only view of m.

Compare with Similar Methods

Related maps:

MethodHow it differs
unmodifiableSortedMap(m)Also keeps firstKey and range views.
singletonMap(k, v)Immutable map with one entry.
synchronizedMap(m)Thread-safe, still changeable.

Java Examples

Example 1 — Read Configuration Through a Read-Only Map

Look up existing and missing settings through the view.

Java
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"));
    }
}

How It Works

  1. Wrapped on the way out. load fills a TreeMap and returns only the read-only view, so the original map is unreachable.
  2. Normal reads. get("theme") returns "dark", and the map prints in key order.
  3. 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.

Java
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);
    }
}

How It Works

  1. Classic write. put of a new key is refused.
  2. Java 8 defaults too. merge would add 1 to apple's count; the view overrides it to throw instead.
  3. 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.

Java
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());
        }
    }
}

How It Works

  1. Locked keys, open values. view.get("red") returns the real ArrayList, and adding "ben" to it is allowed.
  2. Wrap each value. The second map stores unmodifiableList views as its values before being wrapped itself.
  3. Two layers. Now both the map and each team list refuse changes, so adding "cy" throws.

Frequently Asked Questions

It returns a read-only view of a Map. Lookups work; put, remove, merge, and similar methods throw UnsupportedOperationException.
No. Only the mappings are locked. Mutable values, such as lists stored in the map, can still be modified.
Yes. It only reads, so it behaves like on the original map.
Yes, if someone modifies the original map. The view reflects those changes.
Map.copyOf (Java 10+) makes an immutable copy and rejects nulls; unmodifiableMap is a live view and works on any Java version.

Next: unmodifiableNavigableMap()

Wrap a NavigableMap in a read-only view that still supports floorEntry and ceilingKey.

unmodifiableNavigableMap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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