Java : EnumSet with Examples

EnumSet (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the EnumSet<E extends Enum<E>> methods.


Summary

A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created.

Class diagram

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY]
System.out.println(set.size()); // 3

System.out.println(set.contains(DayOfWeek.MONDAY)); // true
System.out.println(set.contains(DayOfWeek.TUESDAY)); // true
System.out.println(set.contains(DayOfWeek.SUNDAY)); // false

Methods

static <E extends Enum<E>> EnumSet<E> allOf (Class<E> elementType)

Creates an enum set containing all of the elements in the specified element type.

final var set = EnumSet.allOf(DayOfWeek.class);

// [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
System.out.println(set);

EnumSet<E> clone ()

Returns a copy of this set.

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY]

final var cloned = set.clone();
System.out.println(cloned); // [MONDAY, TUESDAY, WEDNESDAY]

static <E extends Enum<E>> EnumSet<E> complementOf (EnumSet<E> s)

Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.

final var src = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(src); // [MONDAY, TUESDAY, WEDNESDAY]

final var set = EnumSet.complementOf(src);
System.out.println(set); // [THURSDAY, FRIDAY, SATURDAY, SUNDAY]

static <E extends Enum<E>> EnumSet<E> copyOf (Collection<E> c)

Creates an enum set initialized from the specified collection.

final Collection<DayOfWeek> c = List.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(c); // [MONDAY, TUESDAY, WEDNESDAY]

final var set = EnumSet.copyOf(c);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY]

static <E extends Enum<E>> EnumSet<E> copyOf (EnumSet<E> s)

Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).

final var s = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(s); // [MONDAY, TUESDAY, WEDNESDAY]

final var set = EnumSet.copyOf(s);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY]

static <E extends Enum<E>> EnumSet<E> noneOf (Class<E> elementType)

Creates an empty enum set with the specified element type.

final var set = EnumSet.noneOf(DayOfWeek.class);
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

set.add(DayOfWeek.MONDAY);
System.out.println(set); // [MONDAY]
System.out.println(set.size()); // 1

set.add(DayOfWeek.TUESDAY);
System.out.println(set); // [MONDAY, TUESDAY]
System.out.println(set.size()); // 2

static <E extends Enum<E>> EnumSet<E> of (E e)

Creates an enum set initially containing the specified element.

final var set = EnumSet.of(DayOfWeek.MONDAY);
System.out.println(set); // [MONDAY]
System.out.println(set.size()); // 1

static <E extends Enum<E>> EnumSet<E> of (E e1, E e2)

Creates an enum set initially containing the specified elements.

final var set = EnumSet.of(DayOfWeek.MONDAY, DayOfWeek.TUESDAY);
System.out.println(set); // [MONDAY, TUESDAY]
System.out.println(set.size()); // 2

static <E extends Enum<E>> EnumSet<E> of (E first, E... rest)

Creates an enum set initially containing the specified elements.

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY,
        DayOfWeek.THURSDAY, DayOfWeek.FRIDAY, DayOfWeek.SATURDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
System.out.println(set.size()); // 6

static <E extends Enum<E>> EnumSet<E> of (E e1, E e2, E e3)

Creates an enum set initially containing the specified elements.

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY]
System.out.println(set.size()); // 3

static <E extends Enum<E>> EnumSet<E> of (E e1, E e2, E e3, E e4)

Creates an enum set initially containing the specified elements.

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY,
        DayOfWeek.THURSDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY, THURSDAY]
System.out.println(set.size()); // 4

static <E extends Enum<E>> EnumSet<E> of (E e1, E e2, E e3, E e4, E e5)

Creates an enum set initially containing the specified elements.

final var set = EnumSet.of(
        DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY,
        DayOfWeek.THURSDAY, DayOfWeek.FRIDAY);
System.out.println(set); // [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
System.out.println(set.size()); // 5

static <E extends Enum<E>> EnumSet<E> range (E from, E to)

Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

final var set = EnumSet.range(DayOfWeek.TUESDAY, DayOfWeek.FRIDAY);
System.out.println(set); // [TUESDAY, WEDNESDAY, THURSDAY, FRIDAY]
System.out.println(set.size()); // 4

Methods declared in AbstractSet

equals, hashCode, removeAll

Please see the link below.

Methods declared in AbstractCollection

add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, toArray, toArray, toString

Please see the link below.

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Set

add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, retainAll, size, spliterator, toArray, toArray

Please see the link below.


Related posts

To top of page