Java EnumSet - of() Method
The java.util.EnumSet.of() method is used to create an enum set initially containing the specified element.
Syntax
public static <E extends Enum<E>> EnumSet<E> of(E e)
Here, E is the type of element maintained by the container.
Parameters
e |
Specify the element that this set is to contain initially. |
Return Value
Returns an enum set initially containing the specified element.
Exception
Throws NullPointerException, if e is null.
Example:
In the example below, the java.util.EnumSet.of() method is used to create an enum set initially containing the specified element.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ SUN, MON, TUE, WED, THU, FRI, SAT } public static void main(String[] args) { //creating an EnumSet EnumSet<weekday> Set; //Add one element using of() method Set = EnumSet.of(weekday.SUN); //print the content of Set System.out.println("Set contains: "+ Set); //Add another element using of() method //which replaces the previous element Set = EnumSet.of(weekday.FRI); //print the content of Set System.out.println("Set contains: "+ Set); } }
The output of the above code will be:
Set contains: [SUN] Set contains: [FRI]
❮ Java.util - EnumSet