Map.of is unmodifiable and rejects null keys and values. Its iteration order is deliberately unspecified, so wrap it in a TreeMap when you need to print it predictably.
var config = Map.ofEntries(
Map.entry("host", "localhost"),
Map.entry("port", "8089"),
Map.entry("ssl", "false"));
System.out.println(new TreeMap<>(config));
try {
config.put("extra", "x");
} catch (UnsupportedOperationException e) {
System.out.println("Map.of is unmodifiable");
}
try {
Map.of("k", null);
} catch (NullPointerException e) {
System.out.println("Map.of rejects null values");
}
{host=localhost, port=8089, ssl=false}
Map.of is unmodifiable
Map.of rejects null values
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30