Java Collections singletonMap() Method

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

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.

Java
Map<String, Integer> limits = Collections.singletonMap("maxUsers", 50);   // {maxUsers=50}
Integer max = limits.get("maxUsers");                                       // 50
Integer other = limits.get("timeout");                                      // null
CallWhat 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
// java.util.Collections
public static <K,V> Map<K,V> singletonMap(K key, V value)

Parameters

ParameterDescription
keyThe only key. May be null.
valueThe value for that key. May be null.

Return Value

Return ValueDescription
Map<K,V>An immutable map with exactly one entry.

Compare with Similar Methods

Small immutable maps:

MethodHow it differs
emptyMap()Immutable map with no entries.
singleton(x)One-element Set.
unmodifiableMap(m)Read-only view of a larger map.
Map.of(k, v)Java 9+; no null allowed.

Java Examples

Example 1 — Fill a Template with One Value

A tiny template method replaces {key} markers using a map. Pass a single value with singletonMap.

Java
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

  1. Map parameter. render loops over any map's entries and replaces each {key} marker.
  2. One entry is enough. singletonMap("name", "Ana") supplies the only value the template needs.
  3. 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.

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

How It Works

  1. Lookups. "level" maps to 3; "speed" is not a key, so get returns null.
  2. No new keys. put would add a second entry, so it throws UnsupportedOperationException.
  3. 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.

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

How It Works

  1. Seed a TreeMap. The TreeMap copy constructor takes the single entry ana=10.
  2. Normal map from here on. Adding ben and overwriting ana both work on the copy, which keeps keys in sorted order.
  3. null key. singletonMap(null, "no key") is legal; get(null) finds the value. (A TreeMap would reject a null key.)

Frequently Asked Questions

An immutable Map that contains exactly one key-value pair.
No. put throws UnsupportedOperationException. Copy it into a new TreeMap, LinkedHashMap, or HashMap first.
No. Both put with the same key and setValue on its entry throw UnsupportedOperationException.
Yes, both. Map.of in Java 9+ does not.
When a method takes a Map but you only need to pass one option, parameter, or placeholder value.

Next: sort()

Sort a list in natural order or with a Comparator.

sort() 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.

12 people found this page helpful