RegularEnumSet vs JumboEnumSet in Java

Hello guys, the difference between RegularEnumSet and JumboEnumSet in Java was asked in a recent Java interview with one of my friends, 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 with me, I really liked it because, despite the usefulness of EnumSet and its fast implementation, not many developers know about it, despite being mentioned in Java's 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 implementations, which is used to store Java Enum

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

By the way, if you are new to the Java programming world and not familiar with essential Java APIs then I suggest you join The Complete Java Masterclass course on Udemy. This 80-hour course is the best way to learn Java online in-depth. 






How EnumSet is implemented in Java

EnumSet is an abstract class and it provides two concrete implementations, java.util.RegularEnumSet and java.util.JumboEnumSet. The main difference between RegularEnumSet and JumboEnumSet is that the former uses a long variable to store elements while later uses a long[] to store its element. 

Since RegularEnumSet uses a long variable, which is a 64-bit data type, it can only hold that much of an element. 

That's why when an empty EnumSet is created using EnumSet.noneOf() method, it chooses RegularEnumSet if the key universe (number of enum instances in Key Enum) is less than or equal to 64 and JumboEnumSet if the 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);
        else
            return new JumboEnumSet<E>(elementType, universe);
 
    }

Though it's a pretty low-level implementation detail, it's good to know about it to impress the Interviewer, if you happened to ask the same question in your interview. 

By the way, knowledge of essential Java APIs like Java Collection Framework is very important, not just from an interview point of view but also from day-to-day Java development work. 

If you want to learn more about Java Collection Framework then I suggest you join Java Collection Framework in Detail course on Udemy. This 9-hour course is great to learn many useful Collection classes. 
RegularEnumSet vs JumboEnumSet in Java



EnumSet 101 for Beginners

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 threads 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 about the difference between RegularEnumSet and JumboEnumSet in Java. As I said, this is a very useful class and has been recommended by Joshua Bloch on the Effective Java book as well. The beauty of EnumSet implementation lies in how they are created. 

This class is purposefully made package-private so that no one can create an instance of EnumSet. you can only create instances of EnumSet by using different factory methods provided by API. This allows API to choose from RegularEnumSet and JumboEnumSet, depending upon a number of instances of Enum i.e. key size.

This arrangement is also very extensible and manageable because you can introduce a 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 Java Collection Articles  you may like
  • My favorite Courses to learn Java Collections (best courses)
  • 5 Difference between HashMap and Hashtable in Java (answer)
  • 10 Advanced Core Java Courses for Programmers (advanced courses)
  • How to sort HashMap in Java by keys (example)
  • How to sort HashMap by values in Java 8? (example)
  • 5 Free Java Programming Courses for Beginners (free courses)
  • How to get key from HashMap  by passing values (example)
  • 50+ Java Collection Interview Questions with Answers (questions)
  • 10 Example of Concurrent HashMap in Java (examples)
  • Difference between ConcurrentHashMap and HashMap in Java (answer)
  • 21 HashMap Interview questions for Java Programmers (questions)
  • 5 Best Courses to learn Java Collection Framework (courses)
  • 2 ways to sort HashMap in Java? (examples)
  • Difference between HashMap, LinkedHashMap, and TreeMap in Java (answer)

Thanks a lot for reading this article so far. If you find this article useful then please share it with your friends and colleagues. If you have any questions or doubts, feel free to ask.

P. S. - If you are new to the Java Programming world and looking for a free resource to learn Java then you can also check out this Java Tutorial for Complete Beginners (FREE) course on Udemy. This course is completely free and more than 1.2 million people have joined this course. 

2 comments:

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

    ReplyDelete
    Replies
    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.

      Delete

Feel free to comment, ask questions if you have any doubt.