Home Java Collections Methods emptyMap() Java Collections emptyMap() Method 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.
Map<String, Integer> none = Collections.emptyMap(); // {}
Integer v = none.get("a"); // null
int size = none.getOrDefault("size", 10); // 10 Call What 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.util.Collections
public static final <K,V> Map<K,V> emptyMap()
public static final Map EMPTY_MAP // raw constant, older style Parameters Parameter Description None emptyMap() takes no parameters.
Return Value Return Value Description Map<K,V>An empty, immutable map.
Important
put, putAll with entries, putIfAbsent, merge, and the compute methods throw UnsupportedOperationException. Its keySet(), values(), and entrySet() are empty too.
Compare with Similar Methods Related map factories:
Java Examples 1. Lookups 2. Cannot change 3. No overrides Example 1 — Every Lookup Misses Query an empty settings map with get, getOrDefault, and containsKey.
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"));
}
} map: {} size 0
get(width): null
getOrDefault(width, 80): 80
containsKey(width): false How It Works Prints as braces. An empty map's toString is {}.get misses. No key exists, so get returns null; unboxing that into an int would throw.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.
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());
}
} put: UnsupportedOperationException
putIfAbsent: UnsupportedOperationException
keySet [], entrySet empty: true How It Works Classic put. put is a change, so the immutable map throws UnsupportedOperationException.Java 8 methods too. putIfAbsent would add an entry when the key is missing, which it always is here, so it also throws.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".
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)));
}
} {size=10, zoom=100}
{size=14, zoom=100} How It Works Copy the defaults. apply starts from a new TreeMap copy, so the caller's defaults are never changed.Nothing to merge. putAll of an empty map adds no entries, so the first call returns the defaults as they are.One override. With singletonMap("size", 14), only the size entry is replaced.Frequently Asked Questions What does Collections.emptyMap() return? An empty, immutable Map. Lookups always miss and changes throw UnsupportedOperationException.
What does get() return on Collections.emptyMap()? null, for every key. Use getOrDefault to supply a fallback value.
Can I put entries into Collections.emptyMap()? No. Create a TreeMap, LinkedHashMap, or HashMap if you need to add entries.
What is the difference between emptyMap() and EMPTY_MAP? Same behavior, but EMPTY_MAP is raw-typed and causes unchecked warnings; emptyMap() is generic.
Should I use Collections.emptyMap() or Map.of()? Both give an immutable empty map. Map.of() needs Java 9+.
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