Java Collection Tutorial - Java EnumSet.of(E e)








Syntax

EnumSet.of(E e) has the following syntax.

public static <E extends Enum <E>> EnumSet <E> of(E e)

Example

In the following code shows how to use EnumSet.of(E e) method.

/*from   w  w  w.j a v a2s.c o  m*/

import java.util.EnumSet;

enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
};
public class Main {

   public static void main(String[] args) {

      // create a set
      EnumSet<Numbers>  set;

      // add one element
      set = EnumSet.of(Numbers.FIVE);

      System.out.println(set);

      // add another element which replaces the previous
      set = EnumSet.of(Numbers.THREE);

      System.out.println(set);
   }
}

The code above generates the following result.