Java.util.EnumSet.of() Method

Advertisements


Description

The java.util.EnumSet.of(E e1,E e2) method creates an enum set initially containing the two specified elements.

Declaration

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

public static <E extends Enum<E>> EnumSet<E> of(E e1,E e2)

Parameters

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

  • e2-- another element that this set is to contain initially.

Return Value

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

Exception

  • NullPointerException-- if any of parameters 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 elements
      set = EnumSet.of(Numbers.FIVE,Numbers.FOUR);

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

   }
}

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

Set:[FOUR,FIVE]



Advertisements
Advertisements