Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Java Map.of() and Map.entry()

Reference for Map.of(), Map.ofEntries(), Map.copyOf() and Map.entry(): compact immutable maps, their null and duplicate rules, and unspecified order.

Map.of() creates a small, unmodifiable map in one expression. It was added in Java 9 and takes alternating keys and values, from zero up to ten pairs. For more pairs use Map.ofEntries() with Map.entry(k, v). Map.copyOf(map) (Java 10) makes an unmodifiable copy of an existing map.

These maps reject null keys and values, reject duplicate keys, and throw UnsupportedOperationException from every mutator. Their iteration order is unspecified and can differ from one JVM run to another, so never print or compare them by order.

Java
Map<String, Integer> limits = Map.of("low", 1, "high", 10);
Map.Entry<String, Integer> e = Map.entry("mid", 5);

Try it

Java Creating immutable maps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java What it refuses to do

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static <K, V> Map<K, V> of()
static <K, V> Map<K, V> of(K k1, V v1, ...)                 // up to 10 pairs
static <K, V> Map<K, V> ofEntries(Map.Entry<? extends K, ? extends V>... entries)
static <K, V> Map<K, V> copyOf(Map<? extends K, ? extends V> map)
static <K, V> Map.Entry<K, V> entry(K k, V v)
Name Description
k1, v1, ... Alternating keys and values. None may be null; keys must be distinct.
entries Entries for ofEntries; build them with Map.entry().
map Map to copy for copyOf.

Returns: an unmodifiable map (or, for entry, an immutable Map.Entry).

Throws: NullPointerException for any null key, value or argument; IllegalArgumentException for duplicate keys; UnsupportedOperationException from mutating methods.

Usage notes

  • Unspecified order: do not rely on any iteration order; it can change between JVM runs. Wrap in a TreeMap or use LinkedHashMap when order matters.
  • Shallow immutability: the mapping cannot change, but mutable values can.
  • Map.copyOf() returns the same instance if the argument is already an unmodifiable map created by these factories, so it is cheap for already-immutable inputs.
  • Map.entry() entries are immutable (setValue throws) and reject nulls. new AbstractMap.SimpleEntry<>(k, v) is the mutable, null-friendly alternative.
  • Collectors.toUnmodifiableMap() builds the same kind of map from a stream.
  • Lookups with null (get(null), containsKey(null)) throw instead of returning null.
  • Memory: compact implementations make these good for constants and lookup tables.
  • Use ofEntries past ten pairs, or when entries are computed.