Java Utility Library

Java EnumSet - noneOf() Method



The java.util.EnumSet.noneOf() method is used to create an empty enum set with the specified element type.

Syntax

public static <E extends Enum<E>> EnumSet<E> noneOf(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 empty enum set of the specified type.

Exception

Throws NullPointerException, if elementType is null.

Example:

In the example below, the java.util.EnumSet.noneOf() method is used to create an empty enum set with 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) {
    //create a EnumSet
    EnumSet<weekday> Set1;

    //populating Set1
    Set1 = EnumSet.allOf(weekday.class);

    //print the Set1
    System.out.println("Set1 contains: "+ Set1); 

    //Create another EnumSet which is empty &
    //same element type as of weekday
    EnumSet<weekday> Set2 = EnumSet.noneOf(weekday.class);
    
    //print the content of Set2
    System.out.println("Set2 contains: "+ Set2); 
  }
}

The output of the above code will be:

Set1 contains: [SUN, MON, TUE, WED, THU, FRI, SAT]
Set2 contains: []

❮ Java.util - EnumSet