Collections Początkujący

Create immutable sets with a single factory call.

✕ Java 8
Set<String> set =
    Collections.unmodifiableSet(
        new HashSet<>(
            Arrays.asList("a", "b", "c")
        )
    );
✓ Java 9+
Set<String> set =
    Set.of("a", "b", "c");
Widzisz problem z tym kodem? Daj nam znać.
📏

Concise

One line instead of three nested calls.

🚫

Detects duplicates

Throws if you accidentally pass duplicate elements.

🔒

Immutable

No add/remove possible after creation.

Stare podejście
Verbose Wrapping
Nowoczesne podejście
Set.of()
Od JDK
9
Poziom trudności
Początkujący
Immutable set creation
Dostępne

Widely available since JDK 9 (Sept 2017)

Set.of() creates a truly immutable set that rejects nulls and duplicate elements at creation time. No more wrapping mutable sets.

Udostępnij 𝕏 🦋 in