Java 9 – Factory Method for Immutable Map: Map.ofEntries()
Java 9 introduced two factory methods for creating immutable maps: Map.of() (covered in an earlier post, limited to 10 entries) and Map.ofEntries(), which has no entry-count limit. This post covers Map.ofEntries() with Map.entry() and explains when and why to use it.
Why Map.ofEntries()?
Map.of(k1,v1, k2,v2, ...) only supports up to 10 key-value pairs because Java can't have a varargs method accepting alternating types cleanly. Map.ofEntries() solves this by accepting an array of Map.Entry objects, allowing any number of entries.
Syntax
Map<K, V> map = Map.ofEntries(
Map.entry(key1, value1),
Map.entry(key2, value2),
// ... any number of entries
);
Example 1 – Simple String Map
package com.java9r;
import java.util.Map;
public class ImmutableMapOfEntries {
public static void main(String[] args) {
Map<Integer, String> products = Map.ofEntries(
Map.entry(1, "Laptop"),
Map.entry(2, "Smartphone"),
Map.entry(3, "Tablet"),
Map.entry(4, "Monitor"),
Map.entry(5, "Keyboard"),
Map.entry(6, "Mouse"),
Map.entry(7, "Headphones"),
Map.entry(8, "Webcam"),
Map.entry(9, "Speaker"),
Map.entry(10, "Charger"),
Map.entry(11, "USB Hub") // 11 entries — not possible with Map.of()
);
System.out.println("Products Map:");
products.forEach((id, name) ->
System.out.println(" " + id + ": " + name));
System.out.println("Total entries: " + products.size());
}
}
Output
Products Map:
1: Laptop
2: Smartphone
3: Tablet
4: Monitor
5: Keyboard
6: Mouse
7: Headphones
8: Webcam
9: Speaker
10: Charger
11: USB Hub
Total entries: 11
Example 2 – Map with Custom Object Values
package com.java9r;
import java.util.Map;
public class ImmutableMapWithObjects {
record Product(String name, double price) {}
public static void main(String[] args) {
Map<String, Product> catalog = Map.ofEntries(
Map.entry("P001", new Product("Laptop", 75000.00)),
Map.entry("P002", new Product("Smartphone", 25000.00)),
Map.entry("P003", new Product("Tablet", 35000.00)),
Map.entry("P004", new Product("Monitor", 15000.00)),
Map.entry("P005", new Product("Keyboard", 2500.00))
);
System.out.println("Product Catalog:");
catalog.forEach((code, p) ->
System.out.printf(" %s → %s (₹%.2f)%n", code, p.name(), p.price()));
// Attempting to modify throws UnsupportedOperationException
try {
catalog.put("P006", new Product("Mouse", 1500.00));
} catch (UnsupportedOperationException e) {
System.out.println("\nCannot modify immutable map: " + e.getClass().getSimpleName());
}
}
}
Output
Product Catalog:
P001 → Laptop (₹75000.00)
P002 → Smartphone (₹25000.00)
P003 → Tablet (₹35000.00)
P004 → Monitor (₹15000.00)
P005 → Keyboard (₹2500.00)
Cannot modify immutable map: UnsupportedOperationException
Map.of() vs Map.ofEntries()
| Feature | Map.of() |
Map.ofEntries() |
|---|---|---|
| Max entries | 10 | Unlimited |
| Syntax | Map.of(k1,v1, k2,v2) |
Map.ofEntries(Map.entry(k1,v1), ...) |
| Null keys/values | Not allowed (throws NPE) | Not allowed (throws NPE) |
| Duplicate keys | Throws IllegalArgumentException |
Throws IllegalArgumentException |
| Iteration order | Not guaranteed | Not guaranteed |
| Mutable? | No | No |
Key Rules for Immutable Maps
- No null keys or values —
Map.entry()itself throwsNullPointerExceptionif either argument is null. - No duplicate keys — adding the same key twice throws
IllegalArgumentExceptionat creation time. - No structural changes —
put(),remove(), andclear()all throwUnsupportedOperationException. - Order not guaranteed — do not rely on iteration order.
Summary
Use Map.ofEntries(Map.entry(k, v), ...) when you need an immutable map with more than 10 entries, or when you prefer the explicit entry syntax for readability. For 10 entries or fewer, Map.of() is slightly more concise. Both create truly immutable maps that throw UnsupportedOperationException on any write attempt — making them safe for use as constants and configuration tables shared across threads.
Comments