How to Create a Map in Java with Map.of
Map.of("free", 10, "pro", 500) creates a Java map from keys and values written one after the other, in one line. The map can't be changed afterwards, doesn't keep the order you wrote, and refuses a key given twice with an IllegalArgumentException.
Syntax
Map<KeyType, ValueType> name = Map.of(key1, value1, key2, value2, ...);
| Part | What it does |
|---|---|
Map.of(...) | Creates an unmodifiable map from up to ten key-value pairs. |
key1, value1, ... | Each key followed by its value. No key can appear twice, and no key or value can be null. |
KeyType, ValueType | The types of the keys and of the values, such as String and Integer. |
name | The variable's name. |
The Map documentation: "The Map.of, Map.ofEntries, and Map.copyOf static factory methods provide a convenient way to create unmodifiable maps." Among their characteristics: "They reject duplicate keys at creation time. Duplicate keys passed to a static factory method result in IllegalArgumentException." And: "The iteration order of mappings is unspecified and is subject to change."
The Example
The example maps two plan names to their limits, then prints the map, the value for pro and the number of entries. A changed version then gives the key pro twice.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> limits = Map.of("free", 10, "pro", 500);
System.out.println(limits);
System.out.println(limits.get("pro"));
System.out.println(limits.size());
}
}
Step 2: Run It
java Main.java
{pro=500, free=10}
500
2
java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell. Your map may print in the other order, for the reason below.
Step 3: Give a Key Twice
Replace Main.java with a version that gives pro a second value:
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> limits = Map.of("free", 10, "pro", 500, "pro", 900);
System.out.println(limits);
}
}
java Main.java
Exception in thread "main" java.lang.IllegalArgumentException: duplicate key: pro
at java.base/java.util.ImmutableCollections$MapN.<init>(ImmutableCollections.java:1440)
at java.base/java.util.Map.of(Map.java:1406)
at Main.main(Main.java:6)

What Happened
- The map printed as
{pro=500, free=10}: each key, an equals sign and its value, between braces. That's not the order of the code. The order of aMap.ofmap can even change from one run to the next: a map of four plans printed{basic=50, team=2000, pro=500, free=10}twice, and then{free=10, pro=500, team=2000, basic=50}. limits.get("pro")returned 500, the value stored under that key.limits.size()returned 2, one for each key.- The changed version stopped with
IllegalArgumentException: duplicate key: probefore it printed anything, becauseprowas given twice.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the entries in the order you wrote them | The map prints in another order, which can change between runs | Use a LinkedHashMap, which keeps the order of put |
A null key or value | NullPointerException, thrown from Objects.requireNonNull inside Map.of | Leave that pair out |
Calling put on a Map.of map | UnsupportedOperationException | Copy it into a map that can change: new HashMap<>(Map.of(...)) |
More than ten pairs in Map.of | error: no suitable method found for of(String,int,...) | Use Map.ofEntries(Map.entry("free", 10), ...), which takes any number |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
java.lang.IllegalArgumentException: duplicate key: pro | The same key is given twice | Remove one of them |
java.lang.NullPointerException at java.util.Objects.requireNonNull, with java.util.Map.of further down the stack trace | A key or a value is null | Leave that pair out |
java.lang.UnsupportedOperationException at java.util.ImmutableCollections.uoe | The code changes a map made with Map.of | Copy it into a HashMap first |
error: no suitable method found for of(...) | Map.of was given eleven or more pairs | Use Map.ofEntries |
FAQ
How many pairs can Map.of take?
Ten. The largest version of Map.of "Returns an unmodifiable map containing ten mappings." Map.ofEntries has no such limit: given eleven entries made with Map.entry, it made a map whose size() was 11.
Can the values have different types?
Yes, when the value type is Object. A Map<String, Object> made with Map.of held the String pro and the Integer 500 side by side.
How do I keep the entries in the order I added them?
Use a LinkedHashMap and add the entries with put. Its documentation says its order "is normally the order in which keys were inserted into the map (insertion-order)", and four plans put into one printed {free=10, pro=500, team=2000, basic=50} on every run.
Conclusion
Map.of makes a small map in one line from up to ten key-value pairs. The map can't change, can't hold null, refuses duplicate keys, and promises no order, so use a LinkedHashMap when the order matters and a HashMap copy when the code needs to change it.
Additional Resources
- Unmodifiable Maps, in the Java 25 API documentation for
Map. - LinkedHashMap, in the Java 25 API documentation.
