Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is EnumSet?
Implementation of EnumSet
EnumSet Methods
Frequently Asked Questions
Is the EnumSet synchronized?
Is EnumSets clonable and serializable?
How many parameters can the of() method contain?
Last Updated: Mar 27, 2024


Master Power BI using Netflix Data
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM


There are a variety of applications that can be built using Java, one of the most popular programming languages. Enumerations are often used in applications to serve a group of named constants. The EnumSet interface can be used if you want to implement the Set interface with an enumeration type in Java. The focus of this article will be on EnumSet.

What is EnumSet?

This class extracts AbstractSet and implements the Set interface. EnumSet overrides many of the methods of AbstractSet and AbstractCollection, even though AbstractSet and AbstractCollection implement the Set and Collection interfaces. The benefit of EnumSet over HashSet is that all methods are implemented using bitwise arithmetic operations; hence it is very fast. EnumSet is very fast since all its basic operations can be performed within a constant amount of time. Bit vectors are used internally to represent enum sets.

Compared with other set implementations (such as HashSet and TreeSet), EnumSet is more efficient at storing enum values. In enum sets, only values for one enum are stored. As a result, the JVM knows all possible values for the set.

Implementation of EnumSet

Many static factory methods are associated with the enumSet class that can be used to create instances. The JDK provides two different implementations:

  1. RegularEnumSet: It represents the bit vector as a single long. The long element consists of a single bit for each enum value. It is pretty easy to tell whether a value is present because the i-th bit will store the value of the enum. This implementation helps to store up to 64 elements since long is a 64-bit data type.
  2. JumboEnumSet: This bit vector uses an array of long elements. It has a maximum number of 64 elements. The array index where the value is stored is calculated slightly differently than that for the RegularEnumSet.

According to the number of elements in an enum, the factory methods of EnumSet generate instances of one implementation or another.

if (a.length <= 64)
    return new RegularEnumSet<>(element_type, a);
    return new JumboEnumSet<>(element_type, a);
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

EnumSet Methods





Constructs an enumerated set that contains all the elements in the type of the element.
clone() This set is returned as a copy.
complementOf(EnumSet<E>) The method creates an enum set with the same element type as the specified set, initializing it with all the elements of this type in the specified set that are not present in this set.
copyOf(EnumSet<E>) Sets up an enum set whose default elements are the same as those in the specified enum set.



Specifies the element type of the enum set that will be created.
of(element) Constructs an array containing the element type specified.
range(element1, element2) The result is an enum set initially filled with every element in the range defined by the endpoints.

Here is a java program to show how all these Enum methods work:

import java.util.*;
public class Main {
    enum faces {
    public static void main(String[] args) {
        // creating a new enumSet using allOf()
        EnumSet < faces > a = EnumSet.allOf(faces.class);
        System.out.println("EnumSet: " + a);

        //creating an empty enumSet using noneOf()
        EnumSet < faces > n = EnumSet.noneOf(faces.class);
        System.out.println("Empty EnumSet: " + n);

        //creating an enumSet using the range()
        EnumSet < faces > r = EnumSet.range(faces.sad, faces.tired);
        System.out.println("Values from Sad to tired: " + r);

        //creating an enumSet having only specific elements using of()
        EnumSet < faces > o = EnumSet.of(faces.happy, faces.angry);
        System.out.println("EnumSet having only happy and angry: " + o);

        //creating an EnumSet having all the enum values
        EnumSet < faces > s1 = EnumSet.allOf(faces.class);

        //creating an empty EnumSet
        EnumSet < faces > n1 = EnumSet.noneOf(faces.class);

        //adding sad value in the empty EnumSet (n1) using add()
        System.out.println("Empty EnumSet after adding a value: " + n1);

        //adding all values from s1 to the empty EnumSet (n1) using addAll()
        System.out.println("Empty EnumSet after adding all the values of other set: " + n1);

        //Iterating the elements
        System.out.println("Iterating the elements of the EnumSet");
        EnumSet < faces > i = EnumSet.allOf(faces.class);
        Iterator < faces > it = i.iterator();
        while (it.hasNext()) {
            System.out.println( + ", ");

        //removing specific values from the enumSet using remove()
        boolean re = i.remove(faces.angry);
        System.out.println("Is the specific value removed?: " + re);

        //removing all the values from the enumSet using removeAll()
        boolean re1 = i.removeAll(i);
        System.out.println("All the values removed?: " + re1);



EnumSet: [happy, sad, angry, tired, confused]
Empty EnumSet: []
Values from Sad to tired: [sad, angry, tired]
EnumSet having only happy and angry: [happy, angry]
Empty EnumSet after adding a value: [sad]
Empty EnumSet after adding all the values of other set: [happy, sad, angry, tired, confused]
Iterating the elements of the EnumSet
Is the specific value removed?: true
All the values removed?: true


Practice it on online java compiler for better understanding.

Frequently Asked Questions

Is the EnumSet synchronized?

Due to its high-performance nature, EnumSet is faster than HashSet because it is not synchronized.

Is EnumSets clonable and serializable?

The EnumSet class implements cloneable and Serializable interfaces. Cloneable allows EnumSet to make copies of its instances. Java objects must be serialized if they are transmitted over a network. Java objects are not capable of being transmitted over the Internet.

How many parameters can the of() method contain?

The of() method accepts up to 5 parameters, such as:
of(e1, e2, e3)
of(e1, e2, e3, e4)
of(e1, e2, e3, e4, e5)


In this blog, we have seen what is EnumSet in the Collection Framework and also the implementation and methods of EnumSet.

We hope that this blog has helped you enhance your knowledge about EnumSet and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Next article
Live masterclass