The java.util.EnumSet.of(E e1,E e2,E e3) method creates an enum set initially containing the three specified elements.
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)
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.
This method returns an enum set initially containing the specified elements.
NullPointerException − if any of parameters is null
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,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]