close
The Wayback Machine - https://web.archive.org/web/20200420041255/https://www.geeksforgeeks.org/enumset-class-java/amp/

EnumSet in Java

Pre-requisite: Enum in Java
EnumSet is one of the specialized implementation of Set interface for use with the enumeration type. Few important features of EnumSet are as follows:

Declaration:

public abstract class EnumSet<E extends Enum<E>> 

Here, E specifies the elements. E must extend Enum, which enforces the requirement that the elements must be of specified enum type.



Below program illustrates a few basic functions of EnumSet:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate working of EnumSet and
// its functions.
import java.util.EnumSet;
     
enum Gfg 
{
    CODE, LEARN, CONTRIBUTE, QUIZ, MCQ
};
public class Example
{
    public static void main(String[] args) 
    {
        // Creating a set
        EnumSet<Gfg> set1, set2, set3, set4;
  
        // Adding elements
        set1 = EnumSet.of(Gfg.QUIZ, Gfg.CONTRIBUTE, 
                          Gfg.LEARN, Gfg.CODE);
        set2 = EnumSet.complementOf(set1);
        set3 = EnumSet.allOf(Gfg.class);
        set4 = EnumSet.range(Gfg.CODE, Gfg.CONTRIBUTE);
        System.out.println("Set 1: " + set1);
        System.out.println("Set 2: " + set2);
        System.out.println("Set 3: " + set3);
        System.out.println("Set 4: " + set4);
    }
}
chevron_right

Output:
Set 1: [CODE, LEARN, CONTRIBUTE, QUIZ]
Set 2: [MCQ]
Set 3: [CODE, LEARN, CONTRIBUTE, QUIZ, MCQ]
Set 4: [CODE, LEARN, CONTRIBUTE]

Methods in EnumSet:


This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.





Improved By : Chinmoy Lenka

Article Tags :
Practice Tags :