Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is EnumSet?
2.1.
Implementation of EnumSet
3.
EnumSet Methods
4.
Frequently Asked Questions
4.1.
Is the EnumSet synchronized?
4.2.
Is EnumSets clonable and serializable?
4.3.
How many parameters can the of() method contain?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

EnumSet

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

Introduction

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);
else
    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
Bootcamp

EnumSet Methods

Methods

Description

allOf(class)

 

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.

noneOf(class)

 

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 {
        happy,
        sad,
        angry,
        tired,
        confused
    }
    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()
        n1.add(faces.sad);
        System.out.println("Empty EnumSet after adding a value: " + n1);

        //adding all values from s1 to the empty EnumSet (n1) using addAll()
        n1.addAll(s1);
        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(it.next() + ", ");
        }

        //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);
    }
}

 

Output:

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
happy, 
sad, 
angry, 
tired, 
confused, 
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)

Conclusion

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
AbstractSet
Next article
TreeSet
Live masterclass