Home Java Collections Methods singletonMap() Java Collections singletonMap() Method Definition In Java, Collections.singletonMap(key, value) returns a Map containing exactly one entry. Like the other singleton factories, it is immutable and very cheap.
It is a quick way to pass "just one option" or "one placeholder value" to a method that takes a Map, without creating a full TreeMap or LinkedHashMap.
A static method of the java.util.Collections utility class, available since Java 1.3. Call it on the class name, as in Collections.singletonMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass the key and the value.
Map<String, Integer> limits = Collections.singletonMap("maxUsers", 50); // {maxUsers=50}
Integer max = limits.get("maxUsers"); // 50
Integer other = limits.get("timeout"); // null Call What it does Collections.singletonMap(k, v)Immutable map {k=v}. map.get(k)Returns v; any other key gives null. new TreeMap<>(Collections.singletonMap(k, v))An editable map that starts with one entry. Map.of(k, v)Java 9+ alternative; rejects null keys and values.
Added in Java 1.3. Both the key and the value may be null. Its keySet(), values(), and entrySet() are one-element immutable views.
Library declarations // java.util.Collections
public static <K,V> Map<K,V> singletonMap(K key, V value) Parameters Parameter Description keyThe only key. May be null. valueThe value for that key. May be null.
Return Value Return Value Description Map<K,V>An immutable map with exactly one entry.
Important
put, remove, clear, and Map.Entry.setValue on its entry throw UnsupportedOperationException. Copy it into a TreeMap, LinkedHashMap, or HashMap when you need to add more entries.
Compare with Similar Methods Small immutable maps:
Java Examples 1. One placeholder 2. Cannot change 3. Editable copy Example 1 — Fill a Template with One Value A tiny template method replaces {key} markers using a map. Pass a single value with singletonMap.
import java.util.Collections;
import java.util.Map;
public class CollectionsSingletonMapTemplate {
static String render(String template, Map<String, String> values) {
String out = template;
for (Map.Entry<String, String> e : values.entrySet()) {
out = out.replace("{" + e.getKey() + "}", e.getValue());
}
return out;
}
public static void main(String[] args) {
Map<String, String> one = Collections.singletonMap("name", "Ana");
System.out.println(render("Hello, {name}!", one));
System.out.println(one);
}
} How It Works Map parameter. render loops over any map's entries and replaces each {key} marker.One entry is enough. singletonMap("name", "Ana") supplies the only value the template needs.Printed form. The map prints like any other map: {name=Ana}.Example 2 — put and setValue Both Fail Look up a present and a missing key, then try to change the map directly and through its entry.
import java.util.Collections;
import java.util.Map;
public class CollectionsSingletonMapRules {
public static void main(String[] args) {
Map<String, Integer> m = Collections.singletonMap("level", 3);
System.out.println("level=" + m.get("level") + ", speed=" + m.get("speed"));
try {
m.put("speed", 9);
} catch (UnsupportedOperationException e) {
System.out.println("put: " + e.getClass().getSimpleName());
}
for (Map.Entry<String, Integer> e : m.entrySet()) {
try {
e.setValue(4);
} catch (UnsupportedOperationException ex) {
System.out.println("setValue: " + ex.getClass().getSimpleName());
}
}
}
} level=3, speed=null
put: UnsupportedOperationException
setValue: UnsupportedOperationException How It Works Lookups. "level" maps to 3; "speed" is not a key, so get returns null.No new keys. put would add a second entry, so it throws UnsupportedOperationException.No back door. The single entry is immutable as well, so setValue(4) through the entry set also throws.Example 3 — Start an Editable Map from One Entry Copy a singleton map into a TreeMap to keep adding, and see that a null key is allowed in the singleton.
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class CollectionsSingletonMapCopy {
public static void main(String[] args) {
Map<String, Integer> scores = new TreeMap<>(Collections.singletonMap("ana", 10));
scores.put("ben", 7);
scores.put("ana", 12);
System.out.println("editable: " + scores);
Map<String, String> odd = Collections.singletonMap(null, "no key");
System.out.println("null key: " + odd + " -> " + odd.get(null));
}
} editable: {ana=12, ben=7}
null key: {null=no key} -> no key How It Works Seed a TreeMap. The TreeMap copy constructor takes the single entry ana=10.Normal map from here on. Adding ben and overwriting ana both work on the copy, which keeps keys in sorted order.null key. singletonMap(null, "no key") is legal; get(null) finds the value. (A TreeMap would reject a null key.)Frequently Asked Questions What does Collections.singletonMap() return? An immutable Map that contains exactly one key-value pair.
Can I add entries to a singletonMap? No. put throws UnsupportedOperationException. Copy it into a new TreeMap, LinkedHashMap, or HashMap first.
Can I change the value in a singletonMap? No. Both put with the same key and setValue on its entry throw UnsupportedOperationException.
Does singletonMap() allow null keys or values? Yes, both. Map.of in Java 9+ does not.
When is singletonMap() useful? When a method takes a Map but you only need to pass one option, parameter, or placeholder value.
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.
12 people found this page helpful
Helpful Share Copy link Suggestion