CKTIS / Java
EnumSet: creating sets and working with elements
Create mutable sets of constants from one enum type, and tell apart a membership result from a report that the set changed.
Version: Java SE 21
Syntax
import java.util.EnumSet;
// E extends Enum<E>; static factories:
EnumSet.of(e1) // fixed-arity overloads for 1–5 arguments
EnumSet.of(first, rest) // rest is an E[]; varargs declaration: of(E first, E... rest)
EnumSet.allOf(MyEnum.class) // all constants of MyEnum
EnumSet.noneOf(MyEnum.class) // no constants of MyEnum
// Instance method signatures:
boolean add(E e)
boolean contains(Object o)
boolean remove(Object o)java.util.EnumSet<E> is a mutable set of constants from one enum type. It stores no duplicates, and iteration follows enum declaration order, not insertion order.
The static factory EnumSet.of(...) starts with selected constants. It has fixed-arity overloads for one to five arguments and a varargs overload, of(E first, E... rest), that requires a first constant. EnumSet.allOf(Day.class) takes a class object instead of a constant and starts with every Day constant. To start empty, use EnumSet.noneOf(Day.class), not EnumSet.of(). Unlike the result of Set.of(...), the sets returned by these EnumSet factories are mutable.
add(E e) returns true only when it inserts a missing constant, and remove(Object o) returns true only when it removes a present element. Adding a duplicate or removing an absent element returns false. contains(Object o) returns true when the element is present and leaves the set unchanged.
An EnumSet cannot store null. Calling add(null) throws NullPointerException, but contains(null) and remove(null) both return false. Passing a null constant to EnumSet.of(...) also throws NullPointerException.
Examples
import java.util.EnumSet;
public class EnumSetDemo {
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
enum Permission { READ, WRITE, DELETE }
public static void main(String[] args) {
EnumSet<Day> days = EnumSet.of(Day.MON, Day.TUE, Day.WED, Day.THU, Day.FRI);
EnumSet<Permission> permissions = EnumSet.of(Permission.READ, Permission.WRITE);
EnumSet<Day> allDays = EnumSet.allOf(Day.class);
System.out.println("initial days: " + days);
System.out.println("all days: " + allDays.size());
System.out.println("can delete: " + permissions.contains(Permission.DELETE));
System.out.println("add SAT: " + days.add(Day.SAT));
System.out.println("add SAT again: " + days.add(Day.SAT));
System.out.println("remove SAT: " + days.remove(Day.SAT));
System.out.println("remove SAT again: " + days.remove(Day.SAT));
System.out.println("contains SAT: " + days.contains(Day.SAT));
}
}Result
initial days: [MON, TUE, WED, THU, FRI] all days: 7 can delete: false add SAT: true add SAT again: false remove SAT: true remove SAT again: false contains SAT: false
The example creates a set of five weekdays, a separate set of two permissions, and a set of all seven days. `SAT` starts outside `days`, so only the first addition and the first removal change that set.
Related lessons
- Java · Lesson 10 · Intermediate · Version: 2