Java EnumSet Example

By Arvind Rai, 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.

1. Creating EnumSet

The EnumSet 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.
Java EnumSet Example
Example: Find an enum.
enum Month {
  JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
} 
Find the code snippet to use 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); 
Output
[JAN]
[JAN, FEB]
[JAN, FEB, MAR] 

1.2 Using copyOf

a.
static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) 
Creates an enum set from specified enum set. The newly created enum set will have same element type and will contain the same elements, if any.
If specified enum set, s, is null, then NullPointerException is thrown.
b.
static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) 
Creates an enum set from specified collection. The specified collection must contain at least one element of enum type.
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); 
Output
[JAN, FEB] 

1.3 Using noneOf

static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) 
Creates an empty enum set with the specified element type.
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) 
Creates an enum set containing all of the elements in the specified element type.
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
} 
Find the code to use allOf method of EnumSet.
EnumSet<Month> e = EnumSet.allOf(Month.class);
System.out.println(e.size());
System.out.println(e); 
Output
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) 
Creates an enum set containing all the elements in the range defined by 'from' and 'to' endpoints. The returned enum set will contain the specified endpoints themselves.
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); 
Output
8
[MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT] 

1.6 Using complementOf

static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) 
Creates an enum set with the same element type as specified enum set, initially containing all the elements of this type excluding the elements in specified enum set.
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); 
Output
[JAN, FEB, MAR, APR, MAY]
[JUN, JUL, AUG, SEP, OCT, NOV, DEC] 

1.7 Using clone

EnumSet<E> clone() 
Returns a copy of this set.
Example:
EnumSet<Month> e = EnumSet.of(Month.JAN, Month.FEB, Month.MAR);
EnumSet<Month> c = e.clone();
System.out.println(c); 
Output
[JAN, FEB, MAR] 

2. Synchronizing EnumSet

The EnumSet 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. The EnumSet 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 + " ")); 
Output
APR AUG DEC 
--Remove--
APR DEC 

4. Iteration Order

When we iterate EnumSet, 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 + " ")); 
Output
JAN MAR MAY 
We can see that the iteration order does not depend on the order in which elements are added in enum set. It follows the natural order of enum constants in which they are defined.

5. Reference

Class EnumSet
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us