Java.util.EnumSet.of() Method

Advertisements


Description

The java.util.EnumSet.of(E e) method creates an enum set initially containing the specified element.

Declaration

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

public static <E extends Enum<E>> EnumSet<E> of(E e)

Parameters

  • e-- the element that this set is to contain initially.

Return Value

This method returns an enum set initially containing the specified element.

Exception

  • NullPointerException-- if e is null

Example

The following example shows the usage of java.util.EnumSet.Of() method.

package com.tutorialspoint;

import java.util.*;

public class EnumSetDemo {

   // create an enum
   public enum Numbers {

      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {

      // create a set
      EnumSet<Numbers> set;

      // add one element
      set = EnumSet.of(Numbers.FIVE);

      // print the set
      System.out.println("Set:" + set);

      // add another element which replaces the previous
      set = EnumSet.of(Numbers.THREE);

      // print the set. Notice that it has one element
      System.out.println("Set:" + set);
   }
}

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

Set:[FIVE]
Set:[THREE]



Advertisements
Advertisements