in , ,

Java EnumSet

Java EnumSet

Java EnumSet

In this tutorial, we will learn about the Java EnumSet class and its different methods with the help of examples.

The EnumSet class of the Java collections system gives a set usage of components of a single enum.

Before you learn about EnumSet, make sure to know about Java Enums.

It implements the Set interface.


Making EnumSet

To make an enum set, we should import the java.util.EnumSet package first.

Unlike to other set executions, the enum set doesn’t have public constructors. We should utilize the predefined techniques to make an enum set.

1. Using allOf(Size)

The allof() method creates an enum set that contains all the values of the specified enum type Size. For example,

import java.util.EnumSet;

class Main {
    // an enum named Size
    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }
    
    public static void main(String[] args) {

        // Creating an EnumSet using allOf()
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);

        System.out.println("EnumSet: " + sizes);
    }

}

Output

EnumSet: [SMALL, MEDIUM, LARGE, EXTRALARGE]

Notice the statement,

EnumSet<Size> sizes = EnumSet.allOf(Size.class);

Here, Size.class denotes the Size enum that we have created.


2. Using noneOf(Size)

The noneOf() method creates an empty enum set. For example,

import java.util.EnumSet;

class Main {

     // an enum type Size
    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Creating an EnumSet using noneOf()
        EnumSet<Size> sizes = EnumSet.noneOf(Size.class);

        System.out.println("Empty EnumSet: " + sizes);
    }
}

Output

Empty EnumSet : []

Here, we have created an empty enum named sizes.

Note: We can only insert elements of enum type Size in the above program. It’s because we created our empty enum set using Size enum.


3. Using range(e1, e2) Method

The range() method makes an enum set containing all the values of an enum somewhere in the range of e1 and e2 including the two qualities. For instance,

import java.util.EnumSet;

class Main {

    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Creating an EnumSet using range()
        EnumSet<Size> sizes = EnumSet.range(Size.MEDIUM, Size.EXTRALARGE);

        System.out.println("EnumSet: " + sizes);
    }
}

Output

EnumSet: [MEDIUM, LARGE, EXTRALARGE]

4. Using of() Method

The of() method makes an enum set containing the predefined components. For instance,

import java.util.EnumSet;

class Main {

    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Using of() with a single parameter
        EnumSet<Size> sizes1 = EnumSet.of(Size.MEDIUM);
        System.out.println("EnumSet1: " + sizes1);

        EnumSet<Size> sizes2 = EnumSet.of(Size.SMALL, Size.LARGE);
        System.out.println("EnumSet2: " + sizes2);
    }
}

Output

EnumSet1: [MEDIUM]
EnumSet2: [SMALL, LARGE]

Methods of EnumSet

The EnumSet class provides methods that allow us to perform various elements on the enum set.


Insert Elements to EnumSet

  • add() – inserts specified enum values to the enum set
  • addAll() inserts all the elements of the specified collection to the set

For example,

import java.util.EnumSet;

class Main {

    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Creating an EnumSet using allOf()
        EnumSet<Size> sizes1 = EnumSet.allOf(Size.class);

        // Creating an EnumSet using noneOf()
        EnumSet<Size> sizes2 = EnumSet.noneOf(Size.class);

        // Using add method
        sizes2.add(Size.MEDIUM);
        System.out.println("EnumSet Using add(): " + sizes2);

        // Using addAll() method
        sizes2.addAll(sizes1);
        System.out.println("EnumSet Using addAll(): " + sizes2);
    }
}

Output

EnumSet using add(): [MEDIUM]
EnumSet using addAll(): [SMALL, MEDIUM, LARGE, EXTRALARGE]

In the above example, we have used the addAll() strategy to insert all the components of an enum set sizes1 to an enum set sizes2.

It’s likewise conceivable to insert components from different assortments, for example, ArrayList, LinkedList, and so on to an enum set using addAll(). However, all assortments ought to be of the equivalent enum type.


Access EnumSet Elements

To get to components of an enum set, we can use the iterator() method. To use this strategy, we should import the java.util.Iterator package. For instance,

import java.util.EnumSet;
import java.util.Iterator;

class Main {

    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Creating an EnumSet using allOf()
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);

        Iterator<Size> iterate = sizes.iterator();

        System.out.print("EnumSet: ");
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}

Output

EnumSet: SMALL, MEDIUM, LARGE, EXTRALARGE,

Note:

  • hasNext() returns true if there is a next element in the enum set
  • next() returns the next element in the enum set

Remove EnumSet Elements

  • remove() – removes the specified element from the enum set
  • removeAll() – removes all the elements from the enum set

For example,

import java.util.EnumSet;

class Main {

    enum Size {
        SMALL, MEDIUM, LARGE, EXTRALARGE
    }

    public static void main(String[] args) {

        // Creating EnumSet using allOf()
        EnumSet<Size> sizes = EnumSet.allOf(Size.class);
        System.out.println("EnumSet: " + sizes);

        // Using remove()
        boolean value1 = sizes.remove(Size.MEDIUM);
        System.out.println("Is MEDIUM removed? " + value1);

        // Using removeAll()
        boolean value2 = sizes.removeAll(sizes);
        System.out.println("Are all elements removed? " + value2);
    }
}

Output

EnumSet: [SMALL, MEDIUM, LARGE, EXTRALARGE]
Is MEDIUM removed? true
Are all elements removed? true

Other Methods

MethodDescription
copyOf()Creates a copy of the EnumSet
contains()Searches the EnumSet for the specified element and returns a boolean result
isEmpty()Checks if the EnumSet is empty
size()Returns the size of the EnumSet
clear()Removes all the elements from the EnumSet

Clonable and Serializable Interfaces

The EnumSet class also implements Cloneable and Serializable interfaces.

Cloneable Interface

It allows the EnumSet class to make a copy of instances of the class.

Serializable Interface

At whatever point Java objects should be transmitted over a network, objects should be changed over into bits or bytes. This is on the grounds that Java objects can’t be transmitted over the network.

The Serializable interface allows classes to be serialized. This implies objects of the classes executing Serializable can be changed over into bits or bytes.


Why EnumSet?

The EnumSet gives a proficient method to store enum values than other set executions (like HashSet, TreeSet).

An enum set just stores enum values of a particular enum. Subsequently, the JVM definitely knows all the potential values of the set.

This is the reason why enum sets are inside executed as a succession of bits. Bits determine if components are available in the enum set or not.

The bit of a comparing component is turned on if that component is available in the set.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java HashSet Class

Java HashSet Class

Java LinkedHashSet

Java LinkedHashSet