Java.util.EnumSet.of() Method

Advertisements


Description

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

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.

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

  • e5-- 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 one element
      set = EnumSet.of(Numbers.FIVE,Numbers.FOUR,Numbers.THREE,Numbers.TWO,Numbers.ONE);

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

   }
}

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

Set:[ONE,TWO,THREE,FOUR,FIVE]



Advertisements
Advertisements