Java Utility Library

Java EnumSet - allOf() Method



The java.util.EnumSet.allOf() method is used to create an enum set containing all of the elements in the specified element type.

Syntax

public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)

Here, E is the type of element maintained by the container.

Parameters

elementType Specify the class object of the element type for this enum set.

Return Value

Returns an enum set containing all the elements in the specified type.

Exception

Throws NullPointerException, if elementType is null.

Example:

In the example below, the java.util.EnumSet.allOf() method is used to create an enum set containing all of the specified element type.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    SUN, MON, TUE, WED, THU, FRI, SAT
  }

  public static void main(String[] args) {
    //creating an EnumSet
    EnumSet<weekday> Set = null;

    //print the content of Set
    System.out.println("Set contains: "+ Set); 

    //Add all elements from weekday
    Set = EnumSet.allOf(weekday.class);
    
    //print the content of Set
    System.out.println("Set contains: "+ Set); 
  }
}

The output of the above code will be:

Set contains: null
Set contains: [SUN, MON, TUE, WED, THU, FRI, SAT]

❮ Java.util - EnumSet