Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

EnumSet.of — create a set of selected enum constants

Create an initially nonempty, mutable set of constants from one enum type. Distinguish collector characteristics from a spliterator’s bit mask.

Version: Java SE 21

Syntax

public static <E extends Enum<E>> EnumSet<E> of(E e)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5)
@SafeVarargs
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

EnumSet.of is a static factory on java.util.EnumSet. It returns a new, mutable EnumSet<E> that starts out nonempty. All constants must belong to the same enum type E; mixing enum types is a compile-time error in type-safe code. Repeating a constant adds it only once, and iteration follows enum declaration order, not argument order.

There are five fixed-arity overloads, taking one through five arguments. The of(E first, E... rest) overload requires a first constant and accepts the remainder as extra arguments or an array. There is no zero-argument of(); use EnumSet.noneOf(Access.class) to start empty. A null constant or a null rest array causes a NullPointerException.

The three-argument EnumSet.of can group three Collector.Characteristics values: CONCURRENT allows multiple threads to call the accumulator on the same container, UNORDERED makes no promise to preserve input encounter order, and IDENTITY_FINISH means the finisher is the identity function. The set alone does not configure a collector. If you implement Collector.characteristics(), return an unmodifiable set rather than the mutable EnumSet directly. By contrast, Spliterator.characteristics() returns an int bit mask describing the spliterator and its elements, not a set of collector enum values.

Examples

import java.util.EnumSet;
import java.util.stream.Collector.Characteristics;

public class Main {
    enum Access { READ, WRITE }

    public static void main(String[] args) {
        EnumSet<Access> readOnly = EnumSet.of(Access.READ);
        EnumSet<Access> readWrite = EnumSet.of(Access.WRITE, Access.READ);
        EnumSet<Characteristics> characteristics = EnumSet.of(
                Characteristics.UNORDERED,
                Characteristics.CONCURRENT,
                Characteristics.IDENTITY_FINISH);

        System.out.println(readOnly);
        System.out.println(readWrite);
        System.out.println(characteristics);
    }
}

Result

[READ]
[READ, WRITE]
[CONCURRENT, UNORDERED, IDENTITY_FINISH]

The first two calls use the one- and two-argument overloads. The second passes `WRITE` before `READ`, but the output follows declaration order. The last call builds only a set of characteristics, not a collector or a spliterator.

Related lessons

  • Java · Lesson 19 · Advanced · Version: 2
  • Java · Lesson 24 · Advanced · Version: 2

Sources