Java EnumSet noneOf() Method



Description

The Java EnumSet noneOf(Class<E> elementType) method creates an empty enum set with the specified element type.

Declaration

Following is the declaration for java.util.EnumSet.noneOf() method

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

Parameters

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

Return Value

This method does not return any value.

Exception

NullPointerException − if elementType is null

Creating a Enumset Without Using given Enum Values Example

The following example shows the usage of Java EnumSet noneOf(Class) method to populate the EnumSet instance. We've created a enum Numbers. Then a EnumSet instance is created using a enum value. Using noneOf(Class) method, another enumSet is populated without values of enum and resulted enumSet is printed.

package com.tutorialspoint;

import java.util.EnumSet;

public class EnumSetDemo {

   // create an enum
   public enum Numbers {
      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {

      // create a set that contains all Numbers class
      EnumSet<Numbers> set1 = EnumSet.allOf(Numbers.class);

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

      // create a second set which is empty
      EnumSet<Numbers> set2 = EnumSet.noneOf(Numbers.class);

      // print the new set
      System.out.println("Set2:" + set2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Set1:[ONE, TWO, THREE, FOUR, FIVE]
Set2:[]

Creating a Enumset Without Using given Enum with Value Example

The following example shows the usage of Java EnumSet noneOf(Class) method to populate the EnumSet instance. We've created a enum Numbers. Then a EnumSet instance is created using a enum value. Using noneOf(Class) method, another enumSet is populated without values of enum and resulted enumSet is printed.

package com.tutorialspoint;

import java.util.EnumSet;

public class EnumSetDemo {

   // create an enum
   public enum Number {
      ONE("1"), TWO("2"), THREE("3"), FOUR("4"), FIVE("5");

      // instance variable for enum
      private String value;

      public String getValue() {
         return this.value;
      }

      // constructor should be private or protected
      private Number(String value) {
         this.value = value;
      }

      @Override
      public String toString() {
        return this.name() +"(" + this.value + ")";
      }
   };

   public static void main(String[] args) {

      // create a set that contains all Numbers class
      EnumSet<Number> set1 = EnumSet.allOf(Number.class);

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

      // create a second set which is empty
      EnumSet<Number> set2 = EnumSet.noneOf(Number.class);

      // print the new set
      System.out.println("Set2:" + set2);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Set1:[ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5)]
Set2:[]
java_util_enumset.htm
Advertisements