Java Collections: EnumSet with Example

Enumset is a special kind of Java Set which is optimized for storing enum constants. Because Java is a strongly typed language, an EnumSet stores exactly one type of enum constants. Since the universe (the range of permitted values)of enum type is always finite, an EnumSet can be optimized in ways that other Set implementations cannot be. A Java program should always use EnumSet when range of values of elements is finite and duplicates are not permitted. EnumSet is based on bit vector data structure and takes advantage of the fact that the range of values of elements is finite. EnumSets are among the less frequently used features of Core Java, and hence are ideal for elimination rounds of job interviews.

Structure of EnumSet class

Among the classes in the Collection Framework EnumSet takes a different approach for instance creation. Usually a Collections class intended to be used in client code supports new object creation via the new operator. EnumSet is intentionally kept as an abstract class and therefore it is not possible to create instances of EnumSet using the new operator. Instead EnumSet has a number of static factory methods (such as of, complementOf, noneOf, copyOf, allOf) that return an instanced of EnumSet. A factory method design pattern allows the class to vary the implementation of a class without changing the interface to the client.
Oracle JDK comes with two concrete implementations of EnumSet – RegularEnumSet and JumboEnumSet. These implementations are package-private and therefore hidden from clients. When a factory method of EnumSet is called an instance of either RegularEnumSet or JumboEnumSet is returned.
All EnumSet implementations have Bit Vectors as the underlying data structure. RegularEnumSet and JumboEnumSet differ from each other in the fact that RegularEnumSet has long variable for the bit vector and JumboEnumSet has an array of long for bit vector implementation. Since a long variable has 64 bits in Java, a RegularEnumSet is returned if the Enum instance is less than 64. If the number of enum constants in the Enum of EnumSet is more than 64, an instance of JumboEnumSet is returned.

Salient Features of EnumSet

The following list captures noteworthy points about EnumSet –

  1. An EnumSet is a specially designed Java Set for Java Enums.
  2. An EnumSet can contain instances of a single enum type. Trying to add more than one type of enum constant results in compile time error.
  3. Iterators returned by EnumSet are weakly consistent, meaning they never throw ConcurrentModificationException. These iterators cannot be used in a multi-threaded environment.
  4. Iterators of EnumSet iterate the elements either in their natural order i.e. the order in which they were declared in the Enum or in order returned by ordinal() method.
  5. EnumSet is an abstract class and provides static factory methods for instance creation. If the number of enum constants in the enum is less than 64 an instance of RegularEnumSet is returned else an instance of JumboEnumSet is returned.
  6. Internally EnumSet is represented as bit vectors.
  7. A null value is not allowed in EnumSet

Example and Demo Program

For sake of illustration, let us try to model a university where each semester a number of courses are offered and each student selects a few of these courses.

The courses offered during a semester are finite number of constants and therefore can be represented as an enum –

Since a student can enroll in a course at most once (it makes no sense to register in a course twice), we can model the collection of courses taken by a student as a Set. As courses are of enum type we can (and should) use EnumSet to store the courses taken by students. This is how our Student class looks like –

In the demo program presented below we store and display courses taken by two students. The complete program is shown below –

The output of the above program is –

That’s all folks!

Leave a comment

Your email address will not be published. Required fields are marked *