Java Utility Library

Java.util.EnumSet Class



Java EnumSet Class

Java.util package provides an EnumSet class which 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. Null elements are not permitted. Attempts to insert a null element will throw NullPointerException.

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.

Class declaration

The declaration of java.util.EnumSet class is:

public abstract class EnumSet<E extends Enum<E>>
  extends AbstractSet<E>
    implements Cloneable, Serializable

java.util.EnumSet Methods

The java.util.EnumSet class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
Creates an enum set containing all of the elements in the specified element type.
2. EnumSet<E> clone()
Returns a copy of this set.
3. static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)
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.
4. static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)
Creates an enum set initialized from the specified collection.
5. static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)
Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).
6. static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
Creates an empty enum set with the specified element type.
7. static <E extends Enum<E>> EnumSet<E> of(E e)
Creates an enum set initially containing the specified element.
8. static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)
Creates an enum set initially containing the specified elements.
9. static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
Creates an enum set initially containing the specified elements.
10. static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)
Creates an enum set initially containing the specified elements.
11. static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4)
Creates an enum set initially containing the specified elements.
12. static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5)
Creates an enum set initially containing the specified elements.
13. static <E extends Enum<E>> EnumSet<E> range(E from, E to)
Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractSet<E>