Friday, March 27, 2020

Difference Betwixt Regularenumset In Addition To Jumboenumset Inward Java

The deviation betwixt RegularEnumSet and JumboEnumSet in Java was asked inward a recent Java interview to 1 of my friend, unfortunately, he hasn't explored this topic good together with couldn't answer this inquiry precisely, only he made certain to larn nearly EnumSet after that. When he discussed this topic to me, I actually liked it because, despite the usefulness of EnumSet together with it's fast implementation, non many developers know nearly it, despite existence mentioned inward Java classic Effective Java. This makes me write this post, where nosotros volition mainly verbalise over a pair of differences betwixt RegularEnumSet together with JumboEnumSet inward Java, only nosotros volition likewise deport upon base of operations upon around of the of import properties of EnumSet.

For those who are completely unknown of EnumSet and wondering what the heck is this novel Set implementation, it's 1 of those really exceptional implementation, which is used to shop Java Enum

Since Enum ever has fixed set out of instances, data-structure which is used to shop Enum tin plow over the sack hold upward optimized depending upon set out of instances together with that's why nosotros receive got ii dissimilar implementation of EnumSet inward Java. We volition receive got a closer await on this concept inward adjacent paragraph.






How EnumSet is implemented inward Java

EnumSet is an abstract class together with it provides ii concrete implementations, java.util.RegularEnumSet together with java.util.JumboEnumSet. Main deviation betwixt RegularEnumSet together with JumboEnumSet is that old uses a long variable to shop elements land later on uses a long[] to shop its element. Since RegularEnumSet uses long variable, which is a 64 flake information type, it tin plow over the sack entirely concord that much of element. That's why when an empty EnumSet is created using EnumSet.noneOf() method, it direct RegularEnumSet if primal universe (number of enum instances inward Key Enum) is less than or equal to 64 together with JumboEnumSet if primal universe is to a greater extent than 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 depression degree implementation detail, it's practiced to know nearly it to print Interviewer, if you lot happened to inquire same inquiry inward your interview.
in Java was asked inward a recent Java interview to 1 of my friend Difference betwixt RegularEnumSet together with JumboEnumSet inward Java
Now let's recap around of the of import properties of EnumSet inward Java

1) EnumSet is non thread-safe, which agency if it needs to hold upward externally synchronized, when multiple thread access it together with 1 of them modifies the Collection.

2) EnumSet tin plow over the sack non hold upward used to shop whatever other object except Enum, at the same fourth dimension you lot tin plow over the sack non shop instances of ii dissimilar Enum.

3) EnumSet doesn't allow Null elements.

4) EnumSet Iterators are fail-safe inward nature.


That's all folks nearly difference inward RegularEnumSet together with JumboEnumSet inward Java. As I said, this is really useful shape together with has been recommended yesteryear Joshua Bloch on Effective Java book equally well. Beauty of EnumSet implementation lies on how they are created. This shape is purposefully made package-private then that no 1 tin plow over the sack create instance of EnumSet. you lot tin plow over the sack entirely create instance of EnumSet yesteryear using dissimilar manufactory methods provided yesteryear API. This allows API to direct from RegularEnumSet together with JumboEnumSet, depending upon set out of instances of Enum i.e. primal size. This organisation is likewise really extensible together with manageable because you lot tin plow over the sack innovate novel EnumSet implementation without breaking customer code.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt HashMap together with Hashtable inward Java
  • Difference betwixt ArrayList together with LinkedList inward Java
  • Difference betwixt TreeSet together with TreeMap inward Java
  • Difference betwixt Vector together with ArrayList inward Java
  • Difference betwixt ConcurrentHashMap together with Hashtable inward Java

  • No comments:

    Post a Comment