EnumSet in Java

In this article, we’ll discuss EnumSet class from collections framework with examples.

What will we Learn?

  1. EnumSet Class Overview
  2. EnumSet Class Method Summary
  3. EnumSet Class Example

1. EnumSet Class Overview

  • EnumSet is a specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient.
  • Space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.
  • The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared). The returned iterator is weakly consistent: it will never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.
  • Null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.
  • Like most collection implementations, EnumSet is not synchronized. If multiple threads access an enum set concurrently, and at least one of the threads modifies the set, it should be synchronized externally.
For Example:
 Set<MyEnum> s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
This class is a member of the Java Collections Framework.

2. EnumSet Class Method Summary


  • static <E extends Enum> EnumSet allOf(Class elementType) - This method creates an enum set containing all of the elements in the specified element type.
  • EnumSet clone() - This method returns a copy of this set.
  • static <E extends Enum> EnumSet complementOf(EnumSet s) - This method creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.
  • static <E extends Enum> EnumSet copyOf(Collection c) - This method creates an enum set initialized from the specified collection.
  • static <E extends Enum> EnumSet copyOf(EnumSet s) - This method creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).
  • static <E extends Enum> EnumSet noneOf(Class elementType) - This method creates an empty enum set with the specified element type.
  • static <E extends Enum> EnumSet of(E e) - This method creates an enum set initially containing the specified element.
  • static <E extends Enum> EnumSet of(E first, E... rest) - This method creates an enum set initially containing the specified elements.
  • static <E extends Enum> EnumSet of(E e1, E e2) - This method creates an enum set initially containing the specified elements.
  • static <E extends Enum> EnumSet of(E e1, E e2, E e3) - This method creates an enum set initially containing the specified elements.
  • static <E extends Enum> EnumSet of(E e1, E e2, E e3, E e4) - This method creates an enum set initially containing the specified elements.
  • static <E extends Enum> EnumSet of(E e1, E e2, E e3, E e4, E e5) - This method creates an enum set initially containing the specified elements.
  • static <E extends Enum> EnumSet range(E from, E to) - This method creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

3. EnumSet Class Example

Let's take an example from Effective Java Book. In this example, we have used basic colors RED, GREEN, and BLUE to create a custom color by using EnumSet
As I said, EnumSet is very good for combining effects, whether it's text styles e.g. BOLD and UNDERLINE, as described in Effective Java, or combining basic colors to create custom color here. If you have MS paint, you can even test the combination of colors e.g. you can combine RED and BLUE to create YELLOW, combine all three to create WHITE, or combining RED and BLUE to create PINK.
import java.util.EnumSet;
import java.util.Set;

public class EnumSetExample {

    public static void main(final String[] args) {
    // this will draw line in yellow color

        final EnumSet<Color> yellow = EnumSet.of(Color.RED, Color.GREEN);
        drawLine(yellow);

        // RED + GREEN + BLUE = WHITE
        final EnumSet<Color> white = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE);
        drawLine(white);
  
        // RED + BLUE = PINK
        final EnumSet<Color> pink = EnumSet.of(Color.RED, Color.BLUE);
        drawLine(pink);

    }

  public static void drawLine(final Set<Color> colors) {
      System.out.println("Requested Colors to draw lines : " + colors);
      for (final Color c : colors) {
          System.out.println("drawing line in color : " + c);
      }
  }

 private enum Color {
     RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255);
     private int r;
     private int g;
     private int b;

     private Color(final int r, final int g, final int b) {
         this.r = r;
         this.g = g;
         this.b = b;
    }

    public int getR() {
        return r;
    }

    public int getG() {
        return g;
    }

    public int getB() {
        return b;
    }
  }
}
Output:
Requested Colors to draw lines : [RED, GREEN]
drawing line in color : RED
drawing line in color : GREEN
Requested Colors to draw lines : [RED, GREEN, BLUE]
drawing line in color : RED
drawing line in color : GREEN
drawing line in color : BLUE
Requested Colors to draw lines : [RED, BLUE]
drawing line in color : RED
drawing line in color : BLUE

Reference

Related Collections Examples

Comments