Difference between RegularEnumSet and JumboEnumSet in Java

The difference between RegularEnumSet and JumboEnumSet in Java was asked in a recent Java interview to one of my friend, unfortunately, he hasn't explored this topic well and couldn't answer this question precisely, but he made sure to learn about EnumSet after that. When he discussed this topic to me, I really liked it because, despite the usefulness of EnumSet and it's fast implementation, not many developers know about it, despite being mentioned in Java classic Effective Java. This makes me write this post, where we will mainly discuss a couple of differences between RegularEnumSet and JumboEnumSet in Java, but we will also touch base upon some of the important properties of EnumSet.

For those who are completely unknown of EnumSet and wondering what the heck is this new Set implementation, it's one of those very special implementation, which is used to store Java Enum

Since Enum always has fixed number of instances, data-structure which is used to store Enum can be optimized depending upon number of instances and that's why we have two different implementation of EnumSet in Java. We will take a closer look on this concept in next paragraph.

How EnumSet is implemented in Java

EnumSet is an abstract class and it provides two concrete implementations, java.util.RegularEnumSet and java.util.JumboEnumSet. Main difference between RegularEnumSet and JumboEnumSet is that former uses a long variable to store elements while later uses a long[] to store its element. Since RegularEnumSet uses long variable, which is a 64 bit data type, it can only hold that much of element. That's why when an empty EnumSet is created using EnumSet.noneOf() method, it choose RegularEnumSet if key universe (number of enum instances in Key Enum) is less than or equal to 64 and JumboEnumSet if key universe is more than 64. Here is the code which does that :

 public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {   
        .. ............ 
        if (universe.length <= 64)
            return new RegularEnumSet<E>(elementType, universe);
            return new JumboEnumSet<E>(elementType, universe);

Though it's pretty low level implementation detail, it's good to know about it to impress Interviewer, if you happened to ask same question in your interview.
EnumSet in Java Difference in RegularEnumSet vs JumboEnumSet
Now let's recap some of the important properties of EnumSet in Java

1) EnumSet is not thread-safe, which means if it needs to be externally synchronized, when multiple thread access it and one of them modifies the Collection.

2) EnumSet can not be used to store any other object except Enum, at the same time you can not store instances of two different Enum.

3) EnumSet doesn't allow Null elements.

4) EnumSet Iterators are fail-safe in nature.

That's all folks about difference in RegularEnumSet and JumboEnumSet in Java. As I said, this is very useful class and has been recommended by Joshua Bloch on Effective Java book as well. Beauty of EnumSet implementation lies on how they are created. This class is purposefully made package-private so that no one can create instance of EnumSet. you can only create instance of EnumSet by using different factory methods provided by API. This allows API to choose from RegularEnumSet and JumboEnumSet, depending upon number of instances of Enum i.e. key size. This arrangement is also very extensible and manageable because you can introduce new EnumSet implementation without breaking client code.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java

Related articles from Java Collection Framework


  1. How EnumSet Iterators are fail-safe in nature when EnumSet is not thread-safe ?

    1. Hello Bibhudatta, The iterator of EnumSet 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. It's not related to thread-safety of EnumSet because generally these kind of iterator work on a separate copy of data, rather than modifying the original copy.