Java.util.EnumSet.of() Method


Advertisements


Description

The java.util.EnumSet.of(E e1,E e2,E e3) method creates an enum set initially containing the three 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,E e3)

Parameters

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

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

  • e3 − 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.

Live Demo
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,Numbers.THREE);

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

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

Set:[THREE,FOUR,FIVE]

java_util_enumset.htm

Advertisements