Java EnumSet is used to store a collection of enum constants and provides high performance and type safety.
In this chapter, you will learn about the Java EnumSet class, its features, and how it is used to work with enum types.
Java EnumSet is a specialized implementation of the Set interface designed specifically for use with enum types. It extends the AbstractSet class and provides a highly efficient way to store and manipulate enum constants.
In the hierarchy, EnumSet extends the AbstractSet class and implements the Set interface, which is a part of the Java Collections Framework. The Set interface further extends the Collection interface, which ultimately derives from the Iterable interface. This hierarchy enables EnumSet to inherit basic set operations while providing a specialized and efficient implementation for enum types.
The hierarchy of the EnumSet class is shown in the figure below:

The following is the declaration of the java.util.EnumSet class:
Java EnumSet provides various methods to perform operations such as creating, modifying, and traversing a set of enum elements.
The following table lists commonly used methods:
| Method | Description |
|---|---|
| static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) | It is used to create an enum set containing all of the elements in the specified element type. |
| static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) | It is used to create an enum set initialized from the specified collection. |
| static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) | It is used to create an empty enum set with the specified element type. |
| static <E extends Enum<E>> EnumSet<E> of(E e) | It is used to create an enum set initially containing the specified element. |
| static <E extends Enum<E>> EnumSet<E> range(E from, E to) | It is used to create an enum set initially containing the specified elements. |
| EnumSet<E> clone() | It is used to return a copy of this set. |
The following examples demonstrate how to use Java EnumSet to store and manipulate enum values.
This example demonstrates how to create an EnumSet and traverse its elements.
Compile and RunOutput:
TUESDAY WEDNESDAY
This example demonstrates how to use allOf() and noneOf() methods of EnumSet.
Compile and RunOutput:
Week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY] Week Days:[]
We request you to subscribe our newsletter for upcoming updates.