return n'th value and key from a hash map in java












0















For my schooling I have to write a programm, which read and return costumer value from data. To do that, I have created an double hash map. In the inner, i have mapped for example, keys like name, to values like chris. The outer map is for mapping the inner maps, to the costumers name.



To create a list of the accessible files/costumers, I have created that code:



for (HashMap<String, String> file: output.MapForFileMaps.values())
{
h = h + 1;
if (fileRequest == h)
{
requestIsListet = true;
for (String key: file.keySet())
{
p = p + 1;
System.out.println("Press " + p + " for " + key);
}
}
}


Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



Current Problem is, that I want to check wheather the input is connected to a accessible part of the map, or not. So I could return this area of code by an while loop, while request is not available.










share|improve this question


















  • 3





    There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

    – Compass
    Nov 20 '18 at 14:37






  • 2





    It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

    – TheJavaGuy-Ivan Milosavljević
    Nov 20 '18 at 14:38











  • What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

    – Ole V.V.
    Nov 20 '18 at 15:09
















0















For my schooling I have to write a programm, which read and return costumer value from data. To do that, I have created an double hash map. In the inner, i have mapped for example, keys like name, to values like chris. The outer map is for mapping the inner maps, to the costumers name.



To create a list of the accessible files/costumers, I have created that code:



for (HashMap<String, String> file: output.MapForFileMaps.values())
{
h = h + 1;
if (fileRequest == h)
{
requestIsListet = true;
for (String key: file.keySet())
{
p = p + 1;
System.out.println("Press " + p + " for " + key);
}
}
}


Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



Current Problem is, that I want to check wheather the input is connected to a accessible part of the map, or not. So I could return this area of code by an while loop, while request is not available.










share|improve this question


















  • 3





    There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

    – Compass
    Nov 20 '18 at 14:37






  • 2





    It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

    – TheJavaGuy-Ivan Milosavljević
    Nov 20 '18 at 14:38











  • What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

    – Ole V.V.
    Nov 20 '18 at 15:09














0












0








0








For my schooling I have to write a programm, which read and return costumer value from data. To do that, I have created an double hash map. In the inner, i have mapped for example, keys like name, to values like chris. The outer map is for mapping the inner maps, to the costumers name.



To create a list of the accessible files/costumers, I have created that code:



for (HashMap<String, String> file: output.MapForFileMaps.values())
{
h = h + 1;
if (fileRequest == h)
{
requestIsListet = true;
for (String key: file.keySet())
{
p = p + 1;
System.out.println("Press " + p + " for " + key);
}
}
}


Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



Current Problem is, that I want to check wheather the input is connected to a accessible part of the map, or not. So I could return this area of code by an while loop, while request is not available.










share|improve this question














For my schooling I have to write a programm, which read and return costumer value from data. To do that, I have created an double hash map. In the inner, i have mapped for example, keys like name, to values like chris. The outer map is for mapping the inner maps, to the costumers name.



To create a list of the accessible files/costumers, I have created that code:



for (HashMap<String, String> file: output.MapForFileMaps.values())
{
h = h + 1;
if (fileRequest == h)
{
requestIsListet = true;
for (String key: file.keySet())
{
p = p + 1;
System.out.println("Press " + p + " for " + key);
}
}
}


Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



Current Problem is, that I want to check wheather the input is connected to a accessible part of the map, or not. So I could return this area of code by an while loop, while request is not available.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 14:32









ChrisChris

82




82








  • 3





    There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

    – Compass
    Nov 20 '18 at 14:37






  • 2





    It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

    – TheJavaGuy-Ivan Milosavljević
    Nov 20 '18 at 14:38











  • What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

    – Ole V.V.
    Nov 20 '18 at 15:09














  • 3





    There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

    – Compass
    Nov 20 '18 at 14:37






  • 2





    It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

    – TheJavaGuy-Ivan Milosavljević
    Nov 20 '18 at 14:38











  • What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

    – Ole V.V.
    Nov 20 '18 at 15:09








3




3





There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

– Compass
Nov 20 '18 at 14:37





There is no guarantee the nth value is going to be the same over multiple iterations for a HashMap. See: stackoverflow.com/questions/2144776/…

– Compass
Nov 20 '18 at 14:37




2




2





It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

– TheJavaGuy-Ivan Milosavljević
Nov 20 '18 at 14:38





It seems you're using wrong data structure for the problem because, by definition, there is no Nth key/value in hash map.

– TheJavaGuy-Ivan Milosavljević
Nov 20 '18 at 14:38













What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

– Ole V.V.
Nov 20 '18 at 15:09





What’s wrong with designing a proper Customer class with instance variables (fields) for name, etc.? In case it helps you. a LinkedHashMap maintains insertion order, so you may use its iterator to count to the nth inserted item (provided that no item has later been removed again).

– Ole V.V.
Nov 20 '18 at 15:09












1 Answer
1






active

oldest

votes


















1














Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



Answer: You COULD, but it wouldnt be efficcient, Hashmaps store elements based on a hash of the key, so the first inserted element isnt per definition on index 0. You can instead use the key to get the value



Take a look here: https://www.geeksforgeeks.org/internal-working-of-hashmap-java/






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%2f53395273%2freturn-nth-value-and-key-from-a-hash-map-in-java%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









    1














    Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



    Answer: You COULD, but it wouldnt be efficcient, Hashmaps store elements based on a hash of the key, so the first inserted element isnt per definition on index 0. You can instead use the key to get the value



    Take a look here: https://www.geeksforgeeks.org/internal-working-of-hashmap-java/






    share|improve this answer




























      1














      Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



      Answer: You COULD, but it wouldnt be efficcient, Hashmaps store elements based on a hash of the key, so the first inserted element isnt per definition on index 0. You can instead use the key to get the value



      Take a look here: https://www.geeksforgeeks.org/internal-working-of-hashmap-java/






      share|improve this answer


























        1












        1








        1







        Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



        Answer: You COULD, but it wouldnt be efficcient, Hashmaps store elements based on a hash of the key, so the first inserted element isnt per definition on index 0. You can instead use the key to get the value



        Take a look here: https://www.geeksforgeeks.org/internal-working-of-hashmap-java/






        share|improve this answer













        Question: Could I reach directly that part of the hash map, which is needed, by asking for the value or key of the hash map on the n`th place?



        Answer: You COULD, but it wouldnt be efficcient, Hashmaps store elements based on a hash of the key, so the first inserted element isnt per definition on index 0. You can instead use the key to get the value



        Take a look here: https://www.geeksforgeeks.org/internal-working-of-hashmap-java/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 14:37









        Teun van der WijstTeun van der Wijst

        570315




        570315






























            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%2f53395273%2freturn-nth-value-and-key-from-a-hash-map-in-java%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory