Difference between RegularEnumSet and JumboEnumSet in Java

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 of usefulness of EnumSet and it's fast implementation, not many developers knows about it, despite being mentioned in Java classic Effective Java. This makes me to write this post, where we will mainly discuss 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 in Java Difference in RegularEnumSet vs JumboEnumSet
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);
        else
            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.

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值