Map.entry() factory
Create map entries with a clean factory method.
Code Comparison
✕ Java 8
Map.Entry<String, Integer> e =
new AbstractMap.SimpleEntry<>(
"key", 42
);
✓ Java 9+
var e = Map.entry("key", 42);
Why the modern way wins
Concise
One line instead of three with a clearer intent.
Immutable
The returned entry cannot be modified.
Composable
Works perfectly with Map.ofEntries() for large maps.
Old Approach
SimpleEntry
Modern Approach
Map.entry()
Since JDK
9
Difficulty
beginner
JDK Support
Map.entry() factory
Available
Widely available since JDK 9 (Sept 2017)
How it works
Map.entry() replaces the verbose AbstractMap.SimpleEntry constructor. It returns an immutable entry, making it ideal for Map.ofEntries() and stream operations.