Java Language Set Factory Method Examples


Example

  • Set<Integer> immutableEmptySet = Set.of();
    • Initializes an empty, immutable Set<Integer>.
  • Set<Integer> immutableSet = Set.of(1, 2, 3, 4, 5);
    • Initializes an immutable Set<Integer> with five initial elements.
  • Set<Integer> mutableSet = new HashSet<>(immutableSet);
    • Initializes a mutable Set<Integer> from an immutable Set<Integer>.