Java Collections emptyMap() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 5+

Definition

In Java, Collections.emptyMap() returns a Map with no key-value pairs that cannot be changed. Every lookup misses: get returns null, containsKey returns false, and getOrDefault returns your default.

Use it as a return value for "no data" and as an argument meaning "no options" or "no overrides". It is immutable and shared, so it is free to use.

A static method of the java.util.Collections utility class, available since Java 5. Call it on the class name, as in Collections.emptyMap(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Call it with no arguments; the key and value types are inferred.

Java
Map<String, Integer> none = Collections.emptyMap();   // {}
Integer v = none.get("a");                             // null
int size = none.getOrDefault("size", 10);              // 10
CallWhat it does
Collections.emptyMap()Shared immutable empty map.
map.get(k)Always null on the empty map.
map.getOrDefault(k, d)Always d on the empty map.
Map.of()Java 9+ immutable empty map.

The raw constant Collections.EMPTY_MAP exists since Java 1.3. The generic emptyMap() was added in Java 5 and avoids unchecked warnings.

Library declarations
Java
// java.util.Collections
public static final <K,V> Map<K,V> emptyMap()

public static final Map EMPTY_MAP   // raw constant, older style

Parameters

ParameterDescription
NoneemptyMap() takes no parameters.

Return Value

Return ValueDescription
Map<K,V>An empty, immutable map.

Compare with Similar Methods

Related map factories:

MethodHow it differs
singletonMap(k, v)Immutable map with exactly one entry.
emptySortedMap()Empty map with SortedMap methods.
unmodifiableMap(m)Read-only view of an existing map.
new TreeMap<>()An empty map you can put into.

Java Examples

Example 1 — Every Lookup Misses

Query an empty settings map with get, getOrDefault, and containsKey.

Java
import java.util.Collections;
import java.util.Map;

public class CollectionsEmptyMapLookups {
    public static void main(String[] args) {
        Map<String, Integer> settings = Collections.emptyMap();

        System.out.println("map: " + settings + " size " + settings.size());
        System.out.println("get(width): " + settings.get("width"));
        System.out.println("getOrDefault(width, 80): " + settings.getOrDefault("width", 80));
        System.out.println("containsKey(width): " + settings.containsKey("width"));
    }
}

How It Works

  1. Prints as braces. An empty map's toString is {}.
  2. get misses. No key exists, so get returns null; unboxing that into an int would throw.
  3. A safer read. getOrDefault hands back 80, the default, which is the usual way to read optional settings.

Example 2 — put and putIfAbsent Both Fail

Try two different ways to add an entry, and look at the empty key and entry views.

Java
import java.util.Collections;
import java.util.Map;

public class CollectionsEmptyMapRules {
    public static void main(String[] args) {
        Map<String, String> none = Collections.emptyMap();

        try {
            none.put("k", "v");
        } catch (UnsupportedOperationException e) {
            System.out.println("put: " + e.getClass().getSimpleName());
        }
        try {
            none.putIfAbsent("k", "v");
        } catch (UnsupportedOperationException e) {
            System.out.println("putIfAbsent: " + e.getClass().getSimpleName());
        }
        System.out.println("keySet " + none.keySet() + ", entrySet empty: " + none.entrySet().isEmpty());
    }
}

How It Works

  1. Classic put. put is a change, so the immutable map throws UnsupportedOperationException.
  2. Java 8 methods too. putIfAbsent would add an entry when the key is missing, which it always is here, so it also throws.
  3. Empty views. The key set prints as [] and the entry set is empty, so a loop over entries does nothing.

Example 3 — Pass "No Overrides" to a Method

A method merges user overrides on top of defaults. Passing emptyMap() means "keep all defaults".

Java
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class CollectionsEmptyMapOverrides {
    static Map<String, Integer> apply(Map<String, Integer> defaults, Map<String, Integer> overrides) {
        Map<String, Integer> result = new TreeMap<>(defaults);
        result.putAll(overrides);
        return result;
    }

    public static void main(String[] args) {
        Map<String, Integer> defaults = new TreeMap<>();
        defaults.put("size", 10);
        defaults.put("zoom", 100);

        System.out.println(apply(defaults, Collections.emptyMap()));
        System.out.println(apply(defaults, Collections.singletonMap("size", 14)));
    }
}

How It Works

  1. Copy the defaults. apply starts from a new TreeMap copy, so the caller's defaults are never changed.
  2. Nothing to merge. putAll of an empty map adds no entries, so the first call returns the defaults as they are.
  3. One override. With singletonMap("size", 14), only the size entry is replaced.

Frequently Asked Questions

An empty, immutable Map. Lookups always miss and changes throw UnsupportedOperationException.
null, for every key. Use getOrDefault to supply a fallback value.
No. Create a TreeMap, LinkedHashMap, or HashMap if you need to add entries.
Same behavior, but EMPTY_MAP is raw-typed and causes unchecked warnings; emptyMap() is generic.
Both give an immutable empty map. Map.of() needs Java 9+.

Next: emptyNavigableMap()

Get an immutable empty NavigableMap (Java 8+).

emptyNavigableMap() 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