Why use EnumMap instead of HashMap












2















As we already have HashMap, why would we use EnumMap?










share|improve this question




















  • 6





    We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

    – Elliott Frisch
    Nov 21 '18 at 6:51






  • 6





    did you read the docs?

    – richflow
    Nov 21 '18 at 6:52






  • 5





    As the doc says This representation is extremely compact and efficient.

    – Evgeni Enchev
    Nov 21 '18 at 6:53
















2















As we already have HashMap, why would we use EnumMap?










share|improve this question




















  • 6





    We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

    – Elliott Frisch
    Nov 21 '18 at 6:51






  • 6





    did you read the docs?

    – richflow
    Nov 21 '18 at 6:52






  • 5





    As the doc says This representation is extremely compact and efficient.

    – Evgeni Enchev
    Nov 21 '18 at 6:53














2












2








2








As we already have HashMap, why would we use EnumMap?










share|improve this question
















As we already have HashMap, why would we use EnumMap?







java hashmap enum-map






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 7:01









Raedwald

26.5k2396157




26.5k2396157










asked Nov 21 '18 at 6:49









Ashish ChoudharyAshish Choudhary

175




175








  • 6





    We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

    – Elliott Frisch
    Nov 21 '18 at 6:51






  • 6





    did you read the docs?

    – richflow
    Nov 21 '18 at 6:52






  • 5





    As the doc says This representation is extremely compact and efficient.

    – Evgeni Enchev
    Nov 21 '18 at 6:53














  • 6





    We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

    – Elliott Frisch
    Nov 21 '18 at 6:51






  • 6





    did you read the docs?

    – richflow
    Nov 21 '18 at 6:52






  • 5





    As the doc says This representation is extremely compact and efficient.

    – Evgeni Enchev
    Nov 21 '18 at 6:53








6




6





We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

– Elliott Frisch
Nov 21 '18 at 6:51





We already have boolean, and digital computer programs are composed of ones and zeros... why do we need any other types?

– Elliott Frisch
Nov 21 '18 at 6:51




6




6





did you read the docs?

– richflow
Nov 21 '18 at 6:52





did you read the docs?

– richflow
Nov 21 '18 at 6:52




5




5





As the doc says This representation is extremely compact and efficient.

– Evgeni Enchev
Nov 21 '18 at 6:53





As the doc says This representation is extremely compact and efficient.

– Evgeni Enchev
Nov 21 '18 at 6:53












2 Answers
2






active

oldest

votes


















6














The Javadoc makes a pretty good argument:




Enum maps are represented internally as arrays. This representation is extremely compact and efficient.



Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.







share|improve this answer
























  • Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

    – jensgram
    Nov 21 '18 at 6:55











  • There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

    – Thilo
    Nov 21 '18 at 6:57





















5














The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.



Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe



1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.



2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.



3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap



int index = ((Enum)key).ordinal();
Object oldValue = vals[index];
vals[index] = maskNull(value);





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',
    autoActivateHeartbeat: false,
    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%2f53406636%2fwhy-use-enummap-instead-of-hashmap%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    The Javadoc makes a pretty good argument:




    Enum maps are represented internally as arrays. This representation is extremely compact and efficient.



    Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.







    share|improve this answer
























    • Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

      – jensgram
      Nov 21 '18 at 6:55











    • There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

      – Thilo
      Nov 21 '18 at 6:57


















    6














    The Javadoc makes a pretty good argument:




    Enum maps are represented internally as arrays. This representation is extremely compact and efficient.



    Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.







    share|improve this answer
























    • Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

      – jensgram
      Nov 21 '18 at 6:55











    • There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

      – Thilo
      Nov 21 '18 at 6:57
















    6












    6








    6







    The Javadoc makes a pretty good argument:




    Enum maps are represented internally as arrays. This representation is extremely compact and efficient.



    Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.







    share|improve this answer













    The Javadoc makes a pretty good argument:




    Enum maps are represented internally as arrays. This representation is extremely compact and efficient.



    Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts.








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 6:53









    ThiloThilo

    196k78422575




    196k78422575













    • Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

      – jensgram
      Nov 21 '18 at 6:55











    • There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

      – Thilo
      Nov 21 '18 at 6:57





















    • Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

      – jensgram
      Nov 21 '18 at 6:55











    • There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

      – Thilo
      Nov 21 '18 at 6:57



















    Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

    – jensgram
    Nov 21 '18 at 6:55





    Pretty much what I was copying :) I suggest OP looks into the source for EnumMap in order to see the shortcuts that can be taken when the entire "usage space" is known in advance (since enums are constant.)

    – jensgram
    Nov 21 '18 at 6:55













    There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

    – Thilo
    Nov 21 '18 at 6:57







    There are also third-party libraries with similar specialized implementations of Map (and other types of Collections) for constrained key or value types such as int, all for the purpose of efficiency.

    – Thilo
    Nov 21 '18 at 6:57















    5














    The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.



    Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe



    1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.



    2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.



    3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap



    int index = ((Enum)key).ordinal();
    Object oldValue = vals[index];
    vals[index] = maskNull(value);





    share|improve this answer




























      5














      The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.



      Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe



      1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.



      2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.



      3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap



      int index = ((Enum)key).ordinal();
      Object oldValue = vals[index];
      vals[index] = maskNull(value);





      share|improve this answer


























        5












        5








        5







        The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.



        Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe



        1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.



        2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.



        3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap



        int index = ((Enum)key).ordinal();
        Object oldValue = vals[index];
        vals[index] = maskNull(value);





        share|improve this answer













        The main reason for EnumMap is that it is specifically optimised for enums. Further benefits are mentioned below.



        Taken help from https://javarevisited.blogspot.com/2012/09/difference-between-enummap-and-hashmap-in-java-vs.html#axzz5XTB1xBUe



        1) First and foremost difference between EnumMap and HashMap is that EnumMap is optimized for enum keys while HashMap is a general purpose Map implementation similar to Hashtable. you can not use any type other than Enum as key in EnumMap but you can use both Enum and any other Object as key in HashMap.



        2) Another difference between EnumMap and HashMap is performance. as discussed in the previous point, due to specialized optimization done for Enum keys, EnumMap is likely to perform better than HashMap when using enum as key object.



        3) One more thing which can be considered as the difference between HashMap and EnumMap is the probability of Collision. Since Enum is internally maintained as array and they are stored in their natural order using ordinal(), as shown in following code which is taken from put() method of EnumMap



        int index = ((Enum)key).ordinal();
        Object oldValue = vals[index];
        vals[index] = maskNull(value);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 6:53









        Pooja AggarwalPooja Aggarwal

        870211




        870211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406636%2fwhy-use-enummap-instead-of-hashmap%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings