Java Generics: Useing multiple generic types in one collection type












2















I would like to use two generic types as a key for a hash map. Effectively:



Map<CollectionType<Integer, Integer>, Character> map = new HashMap<>();


I am trying to find if this is possible, and if so which collection type I could use. The right type needs to be able to accept duplicate values. i.e. <1, 0>, <1, 1>, <2, 0> could all be used as a key in the map



for additional background the key in the map will be coordinates on a hex grid, and the value is what is currently stored at that location.










share|improve this question




















  • 1





    The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

    – shmosel
    Nov 20 '18 at 3:43











  • maybe use a pair: stackoverflow.com/questions/156275/… ?

    – Ray Tayek
    Nov 20 '18 at 3:44











  • Or an Entry. Or make your type.

    – shmosel
    Nov 20 '18 at 3:45











  • can <1, 0> repeat multiple times as key in your case?

    – secret super star
    Nov 20 '18 at 3:55











  • @secretsuperstar no the combination of integers will always be unique.

    – jwmorse
    Nov 20 '18 at 3:59
















2















I would like to use two generic types as a key for a hash map. Effectively:



Map<CollectionType<Integer, Integer>, Character> map = new HashMap<>();


I am trying to find if this is possible, and if so which collection type I could use. The right type needs to be able to accept duplicate values. i.e. <1, 0>, <1, 1>, <2, 0> could all be used as a key in the map



for additional background the key in the map will be coordinates on a hex grid, and the value is what is currently stored at that location.










share|improve this question




















  • 1





    The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

    – shmosel
    Nov 20 '18 at 3:43











  • maybe use a pair: stackoverflow.com/questions/156275/… ?

    – Ray Tayek
    Nov 20 '18 at 3:44











  • Or an Entry. Or make your type.

    – shmosel
    Nov 20 '18 at 3:45











  • can <1, 0> repeat multiple times as key in your case?

    – secret super star
    Nov 20 '18 at 3:55











  • @secretsuperstar no the combination of integers will always be unique.

    – jwmorse
    Nov 20 '18 at 3:59














2












2








2








I would like to use two generic types as a key for a hash map. Effectively:



Map<CollectionType<Integer, Integer>, Character> map = new HashMap<>();


I am trying to find if this is possible, and if so which collection type I could use. The right type needs to be able to accept duplicate values. i.e. <1, 0>, <1, 1>, <2, 0> could all be used as a key in the map



for additional background the key in the map will be coordinates on a hex grid, and the value is what is currently stored at that location.










share|improve this question
















I would like to use two generic types as a key for a hash map. Effectively:



Map<CollectionType<Integer, Integer>, Character> map = new HashMap<>();


I am trying to find if this is possible, and if so which collection type I could use. The right type needs to be able to accept duplicate values. i.e. <1, 0>, <1, 1>, <2, 0> could all be used as a key in the map



for additional background the key in the map will be coordinates on a hex grid, and the value is what is currently stored at that location.







java generics collections






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 4:21









Nicholas K

6,89761133




6,89761133










asked Nov 20 '18 at 3:39









jwmorsejwmorse

133




133








  • 1





    The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

    – shmosel
    Nov 20 '18 at 3:43











  • maybe use a pair: stackoverflow.com/questions/156275/… ?

    – Ray Tayek
    Nov 20 '18 at 3:44











  • Or an Entry. Or make your type.

    – shmosel
    Nov 20 '18 at 3:45











  • can <1, 0> repeat multiple times as key in your case?

    – secret super star
    Nov 20 '18 at 3:55











  • @secretsuperstar no the combination of integers will always be unique.

    – jwmorse
    Nov 20 '18 at 3:59














  • 1





    The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

    – shmosel
    Nov 20 '18 at 3:43











  • maybe use a pair: stackoverflow.com/questions/156275/… ?

    – Ray Tayek
    Nov 20 '18 at 3:44











  • Or an Entry. Or make your type.

    – shmosel
    Nov 20 '18 at 3:45











  • can <1, 0> repeat multiple times as key in your case?

    – secret super star
    Nov 20 '18 at 3:55











  • @secretsuperstar no the combination of integers will always be unique.

    – jwmorse
    Nov 20 '18 at 3:59








1




1





The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

– shmosel
Nov 20 '18 at 3:43





The right type needs to be able to accept duplicate values. What do you mean? Maps don't allow duplicate keys.

– shmosel
Nov 20 '18 at 3:43













maybe use a pair: stackoverflow.com/questions/156275/… ?

– Ray Tayek
Nov 20 '18 at 3:44





maybe use a pair: stackoverflow.com/questions/156275/… ?

– Ray Tayek
Nov 20 '18 at 3:44













Or an Entry. Or make your type.

– shmosel
Nov 20 '18 at 3:45





Or an Entry. Or make your type.

– shmosel
Nov 20 '18 at 3:45













can <1, 0> repeat multiple times as key in your case?

– secret super star
Nov 20 '18 at 3:55





can <1, 0> repeat multiple times as key in your case?

– secret super star
Nov 20 '18 at 3:55













@secretsuperstar no the combination of integers will always be unique.

– jwmorse
Nov 20 '18 at 3:59





@secretsuperstar no the combination of integers will always be unique.

– jwmorse
Nov 20 '18 at 3:59












1 Answer
1






active

oldest

votes


















3














Use a Pair<Integer, Integer>, provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair.



equals() and hashcode() are overridden, so it can work as a key in the Map.



Map<Pair<Integer, Integer>, Character> map = new HashMap<>();





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%2f53385879%2fjava-generics-useing-multiple-generic-types-in-one-collection-type%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









    3














    Use a Pair<Integer, Integer>, provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair.



    equals() and hashcode() are overridden, so it can work as a key in the Map.



    Map<Pair<Integer, Integer>, Character> map = new HashMap<>();





    share|improve this answer




























      3














      Use a Pair<Integer, Integer>, provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair.



      equals() and hashcode() are overridden, so it can work as a key in the Map.



      Map<Pair<Integer, Integer>, Character> map = new HashMap<>();





      share|improve this answer


























        3












        3








        3







        Use a Pair<Integer, Integer>, provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair.



        equals() and hashcode() are overridden, so it can work as a key in the Map.



        Map<Pair<Integer, Integer>, Character> map = new HashMap<>();





        share|improve this answer













        Use a Pair<Integer, Integer>, provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair.



        equals() and hashcode() are overridden, so it can work as a key in the Map.



        Map<Pair<Integer, Integer>, Character> map = new HashMap<>();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 3:46









        KartikKartik

        3,22631435




        3,22631435






























            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%2f53385879%2fjava-generics-useing-multiple-generic-types-in-one-collection-type%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?