Map.entry() factory
Create map entries with a clean factory method.
Porównanie kodu
✕ Java 8
Map.Entry<String, Integer> e =
new AbstractMap.SimpleEntry<>(
"key", 42
);
✓ Java 9+
var e = Map.entry("key", 42);
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
SimpleEntry
Nowoczesne podejście
Map.entry()
Od JDK
9
Poziom trudności
Początkujący
Wsparcie JDK
Map.entry() factory
Dostępne
Widely available since JDK 9 (Sept 2017)
Jak to działa
Map.entry() replaces the verbose AbstractMap.SimpleEntry constructor. It returns an immutable entry, making it ideal for Map.ofEntries() and stream operations.
Powiązana dokumentacja