Need of EnumSet / EnumMap over generified Set / Map











up vote
1
down vote

favorite












EnumSet/EnumMap can be created by specifying the defined enum to produce set/map instance as shown in below sample code.



So far I read, difference between EnumSet/EnumMap with that of Set/Map is that we cannot add objects other than the specified Enumin the EnumSet/EnumMap.
If this is the case, then just the generified Set/Map itself will be enough, isn't it?



Please find the EnumSet/EnumMap and their respective generified Set/Map as follows,



enum Value {
VALUE_1, VALUE_2, VALUE_3
};

public class Sample {
public static void main(String args) {
EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);

Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);

EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);

Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}


So can you please tell me what is the need of having EnumSet/EnumMap eventhough we can able to create the set/map that is generified to the defined Enum?



Thanks in advance.










share|improve this question




























    up vote
    1
    down vote

    favorite












    EnumSet/EnumMap can be created by specifying the defined enum to produce set/map instance as shown in below sample code.



    So far I read, difference between EnumSet/EnumMap with that of Set/Map is that we cannot add objects other than the specified Enumin the EnumSet/EnumMap.
    If this is the case, then just the generified Set/Map itself will be enough, isn't it?



    Please find the EnumSet/EnumMap and their respective generified Set/Map as follows,



    enum Value {
    VALUE_1, VALUE_2, VALUE_3
    };

    public class Sample {
    public static void main(String args) {
    EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);

    Set<Value> enumGenerifiedSet = new HashSet<Value>();
    enumGenerifiedSet.add(Value.VALUE_1);

    EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
    enumMap.put(Value.VALUE_1, 1);

    Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
    enumGenerifiedMap.put(Value.VALUE_1, 1);
    }
    }


    So can you please tell me what is the need of having EnumSet/EnumMap eventhough we can able to create the set/map that is generified to the defined Enum?



    Thanks in advance.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      EnumSet/EnumMap can be created by specifying the defined enum to produce set/map instance as shown in below sample code.



      So far I read, difference between EnumSet/EnumMap with that of Set/Map is that we cannot add objects other than the specified Enumin the EnumSet/EnumMap.
      If this is the case, then just the generified Set/Map itself will be enough, isn't it?



      Please find the EnumSet/EnumMap and their respective generified Set/Map as follows,



      enum Value {
      VALUE_1, VALUE_2, VALUE_3
      };

      public class Sample {
      public static void main(String args) {
      EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);

      Set<Value> enumGenerifiedSet = new HashSet<Value>();
      enumGenerifiedSet.add(Value.VALUE_1);

      EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
      enumMap.put(Value.VALUE_1, 1);

      Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
      enumGenerifiedMap.put(Value.VALUE_1, 1);
      }
      }


      So can you please tell me what is the need of having EnumSet/EnumMap eventhough we can able to create the set/map that is generified to the defined Enum?



      Thanks in advance.










      share|improve this question















      EnumSet/EnumMap can be created by specifying the defined enum to produce set/map instance as shown in below sample code.



      So far I read, difference between EnumSet/EnumMap with that of Set/Map is that we cannot add objects other than the specified Enumin the EnumSet/EnumMap.
      If this is the case, then just the generified Set/Map itself will be enough, isn't it?



      Please find the EnumSet/EnumMap and their respective generified Set/Map as follows,



      enum Value {
      VALUE_1, VALUE_2, VALUE_3
      };

      public class Sample {
      public static void main(String args) {
      EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);

      Set<Value> enumGenerifiedSet = new HashSet<Value>();
      enumGenerifiedSet.add(Value.VALUE_1);

      EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
      enumMap.put(Value.VALUE_1, 1);

      Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
      enumGenerifiedMap.put(Value.VALUE_1, 1);
      }
      }


      So can you please tell me what is the need of having EnumSet/EnumMap eventhough we can able to create the set/map that is generified to the defined Enum?



      Thanks in advance.







      java oop enums set enumset






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 12:09









      Andrew Tobilko

      23.8k84078




      23.8k84078










      asked Nov 9 at 10:52









      parthiban

      1817




      1817
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.



          The documentation is pretty clear on this:




          Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.



          [...]



          null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.



          [...]



          Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.



          java.util.EnumSet, JDK 11







          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224310%2fneed-of-enumset-enummap-over-generified-set-map%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.



            The documentation is pretty clear on this:




            Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.



            [...]



            null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.



            [...]



            Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.



            java.util.EnumSet, JDK 11







            share|improve this answer



























              up vote
              2
              down vote













              The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.



              The documentation is pretty clear on this:




              Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.



              [...]



              null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.



              [...]



              Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.



              java.util.EnumSet, JDK 11







              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.



                The documentation is pretty clear on this:




                Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.



                [...]



                null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.



                [...]



                Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.



                java.util.EnumSet, JDK 11







                share|improve this answer














                The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.



                The documentation is pretty clear on this:




                Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.



                [...]



                null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.



                [...]



                Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.



                java.util.EnumSet, JDK 11








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 at 11:06

























                answered Nov 9 at 11:00









                Andrew Tobilko

                23.8k84078




                23.8k84078






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224310%2fneed-of-enumset-enummap-over-generified-set-map%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Schultheiß

                    Verwaltungsgliederung Dänemarks

                    Liste der Kulturdenkmale in Wilsdruff