Java EnumSet

Java EnumSet class

Last Updated : 29 Mar 2026

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.

What is Java EnumSet?

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.

EnumSet Class Hierarchy

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:

EnumSet class hierarchy

EnumSet Class Declaration

The following is the declaration of the java.util.EnumSet class:

Methods of 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:

MethodDescription
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.

Examples of Java EnumSet Class

The following examples demonstrate how to use Java EnumSet to store and manipulate enum values.

Example 1: Creating and Iterating EnumSet

This example demonstrates how to create an EnumSet and traverse its elements.

Compile and Run

Output:

Example 2: Using allOf() and noneOf() Methods

This example demonstrates how to use allOf() and noneOf() methods of EnumSet.

Compile and Run

Output:

Week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
Week Days:[]