Collections Początkujący

Create map entries with a clean factory method.

✕ 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ć.
📏

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
Map.entry() factory
Dostępne

Widely available since JDK 9 (Sept 2017)

Map.entry() replaces the verbose AbstractMap.SimpleEntry constructor. It returns an immutable entry, making it ideal for Map.ofEntries() and stream operations.

Udostępnij 𝕏 🦋 in