EnumSet in Java
September 17, 2021
On this page we will walk through Java java.util.EnumSet
API.
1. The
EnumSet
is a specialized Set
for use with enum types.
2. The
EnumSet
is used to store enum values.
3. All elements of the enum set must come from a single enum type that is specified at enum set in initialization.
4. When we iterate enum set, it traverses elements in their natural order i.e. the order in which enum constants are declared.
5. Null elements are not permitted in enum set.
6. Enum set is not synchronized. We can synchronize it externally using
Collections.synchronizedSet
.
Contents
1. Creating EnumSet
TheEnumSet
has static methods to initialize and create. Let us discuss these methods of EnumSet
one-by-one.
1.1 Using of
The of
method creates an EnumSet
initially containing the specified elements. Find the Java doc.
enum
.
enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
of
method.
EnumSet<Month> e1 = EnumSet.of(Month.JAN); EnumSet<Month> e2 = EnumSet.of(Month.JAN, Month.FEB); EnumSet<Month> e3 = EnumSet.of(Month.JAN, Month.FEB, Month.MAR); System.out.println(e1); System.out.println(e2); System.out.println(e3);
[JAN] [JAN, FEB] [JAN, FEB, MAR]
1.2 Using copyOf
a.
static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)
If specified enum set, s, is null, then
NullPointerException
is thrown.
b.
static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)
If specified enum set, c, is not an
EnumSet
instance and contains no element, then IllegalArgumentException
is thrown.
If specified collection is null, then
NullPointerException
is thrown.
Example:
EnumSet<Month> e1 = EnumSet.of(Month.JAN, Month.FEB); EnumSet<Month> e2 = EnumSet.copyOf(e1); System.out.println(e2);
[JAN, FEB]
1.3 Using noneOf
static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
There is
NullPointerException
if element type is null.
Example:
EnumSet<Month> e = EnumSet.noneOf(Month.class); System.out.println(e.size()); // 0
1.4 Using allOf
static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
There is
NullPointerException
if element type is null.
Example: Suppose our
enum
is as following.
enum Month { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }
allOf
method of EnumSet
.
EnumSet<Month> e = EnumSet.allOf(Month.class); System.out.println(e.size()); System.out.println(e);
12 [JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC]
1.5 Using range
static <E extends Enum<E>> EnumSet<E> range(E from, E to)
If 'from' or 'to' endpoints are null, there will be
NullPointerException
.
If
from.compareTo(to)
is > 0, then there is IllegalArgumentException
exception.
Example:
EnumSet<Month> e = EnumSet.range(Month.MAR, Month.OCT); System.out.println(e.size()); System.out.println(e);
8 [MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT]
1.6 Using complementOf
static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)
There is
NullPointerException
if specified enum set is null.
Example:
EnumSet<Month> e1 = EnumSet.range(Month.JAN, Month.MAY); EnumSet<Month> e2 = EnumSet.complementOf(e1); System.out.println(e1); System.out.println(e2);
[JAN, FEB, MAR, APR, MAY] [JUN, JUL, AUG, SEP, OCT, NOV, DEC]
1.7 Using clone
EnumSet<E> clone()
Example:
EnumSet<Month> e = EnumSet.of(Month.JAN, Month.FEB, Month.MAR); EnumSet<Month> c = e.clone(); System.out.println(c);
[JAN, FEB, MAR]
2. Synchronizing EnumSet
TheEnumSet
is not synchronized. In multithreading environment, if any of the thread is modifying this enum set, then we can synchronize this enum set externally using Collections.synchronizedSet
.
Example:
Set<Month> e = Collections.synchronizedSet(EnumSet.noneOf(Month.class));
3. Inherited Classes and Interfaces
a. TheEnumSet
extends java.util.AbstractSet
class and inherit its methods such as add
, addAll
, clear
, remove
and contains
etc.
b. The
EnumSet
implements Serializable
, Cloneable
, Iterable
, Collection
and Set
interfaces.
Find examples to use some inherited methods.
EnumSet<Month> e = EnumSet.noneOf(Month.class); e.add(Month.APR); e.add(Month.AUG); e.add(Month.DEC); e.forEach(s -> System.out.print(s + " ")); System.out.println("\n--Remove--"); e.remove(Month.AUG); e.forEach(s -> System.out.print(s + " "));
APR AUG DEC --Remove-- APR DEC
4. Iteration Order
When we iterateEnumSet
, it traverses elements in their natural order i.e. the order in which enum constants are declared. Find the sample code.
EnumSet<Month> e = EnumSet.noneOf(Month.class); e.add(Month.MAY); e.add(Month.JAN); e.add(Month.MAR); e.forEach(s -> System.out.print(s + " "));
JAN MAR MAY