How to iterate through a hashmap to get every fourth key, value pair?











up vote
0
down vote

favorite












I have a hashmap like this;



key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on


Now I want to put this in a ListView which contains four rows, and in row1, I want:



key value1
key1 value5
key2 value9
key3 value13 and so on till end


And in row2, I want:



key value2
key1 value6
key2 value10
key3 value14 and so on till end


And then the same in rows 3 and 4. There are always only 4 rows.



I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.



String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);

for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();

listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}

//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);


Thanks.










share|improve this question




















  • 1




    Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
    – lionscribe
    Nov 5 at 2:35










  • Map itself is not designed to have index. You should not expect a map to keep the items in some order.
    – Vladyslav Matviienko
    Nov 5 at 5:50










  • How is ListView relevant?
    – Boris van Katwijk
    Nov 5 at 13:22










  • @BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
    – Zac
    Nov 6 at 4:12










  • @BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
    – Zac
    Nov 6 at 4:15















up vote
0
down vote

favorite












I have a hashmap like this;



key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on


Now I want to put this in a ListView which contains four rows, and in row1, I want:



key value1
key1 value5
key2 value9
key3 value13 and so on till end


And in row2, I want:



key value2
key1 value6
key2 value10
key3 value14 and so on till end


And then the same in rows 3 and 4. There are always only 4 rows.



I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.



String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);

for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();

listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}

//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);


Thanks.










share|improve this question




















  • 1




    Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
    – lionscribe
    Nov 5 at 2:35










  • Map itself is not designed to have index. You should not expect a map to keep the items in some order.
    – Vladyslav Matviienko
    Nov 5 at 5:50










  • How is ListView relevant?
    – Boris van Katwijk
    Nov 5 at 13:22










  • @BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
    – Zac
    Nov 6 at 4:12










  • @BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
    – Zac
    Nov 6 at 4:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a hashmap like this;



key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on


Now I want to put this in a ListView which contains four rows, and in row1, I want:



key value1
key1 value5
key2 value9
key3 value13 and so on till end


And in row2, I want:



key value2
key1 value6
key2 value10
key3 value14 and so on till end


And then the same in rows 3 and 4. There are always only 4 rows.



I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.



String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);

for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();

listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}

//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);


Thanks.










share|improve this question















I have a hashmap like this;



key value1
key value2
key value3
key value4
key1 value5
key1 value6
key1 value7
key1 value8
key2 value9
... and so on


Now I want to put this in a ListView which contains four rows, and in row1, I want:



key value1
key1 value5
key2 value9
key3 value13 and so on till end


And in row2, I want:



key value2
key1 value6
key2 value10
key3 value14 and so on till end


And then the same in rows 3 and 4. There are always only 4 rows.



I am having trouble iterating through this. Here is the code I wrote, but it doesn't work.



String fourrows = hashMap.get("KEY");
for (int i=0;i<fourrows.length;i++) {
HashMap<String, String> listHashMap = new HashMap<>();
listHashMap.put("TA", "ROW VALUE "+fourrows[i]);

for (int j=1;j<hashMap.entrySet().size();j+=4) {
String val = hashMap.values().toArray()[j].toString();
String key = hashMap.get(hashMap.keySet().toArray()[0]).toString();

listHashMap.put("IA", key);
listHashMap.put("XA", val);
incmStmtList.add(listHashMap);
}
}

//Then I pass TA, IA and XA to a simple list adapter and add it to a listView.
incmAdapter = new SimpleAdapter(getContext(), incmStmtList,R.layout.content_results, new String{"TA", "IA", "XA"},new int{R.id.ta, R.id.ia, R.id.tota});
listView.setVerticalScrollBarEnabled(true);
listView.setAdapter(incmAdapter);


Thanks.







java android loops listview hashmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 6 at 4:17

























asked Nov 5 at 2:13









Zac

3001421




3001421








  • 1




    Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
    – lionscribe
    Nov 5 at 2:35










  • Map itself is not designed to have index. You should not expect a map to keep the items in some order.
    – Vladyslav Matviienko
    Nov 5 at 5:50










  • How is ListView relevant?
    – Boris van Katwijk
    Nov 5 at 13:22










  • @BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
    – Zac
    Nov 6 at 4:12










  • @BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
    – Zac
    Nov 6 at 4:15














  • 1




    Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
    – lionscribe
    Nov 5 at 2:35










  • Map itself is not designed to have index. You should not expect a map to keep the items in some order.
    – Vladyslav Matviienko
    Nov 5 at 5:50










  • How is ListView relevant?
    – Boris van Katwijk
    Nov 5 at 13:22










  • @BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
    – Zac
    Nov 6 at 4:12










  • @BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
    – Zac
    Nov 6 at 4:15








1




1




Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35




Didn't look at your code, but just to note, HashMaps do not keep their insertion order. You may want to use a LinkedHashMap.
– lionscribe
Nov 5 at 2:35












Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50




Map itself is not designed to have index. You should not expect a map to keep the items in some order.
– Vladyslav Matviienko
Nov 5 at 5:50












How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22




How is ListView relevant?
– Boris van Katwijk
Nov 5 at 13:22












@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12




@BorisvanKatwijk ListView contains four rows - each having one particular value of a particular key in the map... inside each row is a couple of textviews, and multiple IA and XA values should be added to each. Hope that clarifies. The question really is how to do that.
– Zac
Nov 6 at 4:12












@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15




@BorisvanKatwijk I have edited the question with how I pass the values to my listView. Thanks.
– Zac
Nov 6 at 4:15












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.



Multimap myList = ArrayListMultimap.create();



            myList.put("key","value1");
myList.put("key","value2");
myList.put("key","value3");
myList.put("key","value4");
myList.put("key1","value5");
myList.put("key1","value6");
myList.put("key1","value7");
myList.put("key1","value8");
myList.put("key2","value9");
myList.put("key2","value10");
myList.put("key2","value11");
myList.put("key2","value12");
myList.put("key3","value13");
myList.put("key3","value14");
myList.put("key3","value15");
myList.put("key3","value16");


Then get all keys from list



  Set myKeySet = myList.keySet();
Object keys = myKeySet.toArray();


Now you have set of all key. then



    HashMap<String, String> listHashMap1 = new HashMap<>();
HashMap<String, String> listHashMap2 = new HashMap<>();
HashMap<String, String> listHashMap3 = new HashMap<>();
HashMap<String, String> listHashMap4 = new HashMap<>();


Now add the value to every list



  for(int i=0;i<4;i++){
List<String> keyval = (List)myList.get((String)keys[i]);
listHashMap1.put((String)keys[i],keyval.get(0));
listHashMap2.put((String)keys[i],keyval.get(1));
listHashMap3.put((String)keys[i],keyval.get(2));
listHashMap4.put((String)keys[i],keyval.get(3));
}


Now you have set of four row list. This Multimap is using google guava.
So you need to implement guava in your gradle as below:
implementation 'com.google.guava:guava:26.0-jre'






share|improve this answer




























    up vote
    0
    down vote













    From the code you supplied it seems that the source hashMap is of type Map<String, String>.



    You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:



        Map<String, String> one = new HashMap<>();
    Map<String, String> two = new HashMap<>();
    Map<String, String> three = new HashMap<>();
    Map<String, String> four = new HashMap<>();

    hashMap.entrySet().forEach(entry -> {
    String values = entry.getValue();
    one.put(entry.getKey(), values[0]);
    two.put(entry.getKey(), values[1]);
    three.put(entry.getKey(), values[2]);
    four.put(entry.getKey(), values[3]);
    });


    Note: assuming every key always has exactly four values.






    share|improve this answer





















    • But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
      – Zac
      Nov 6 at 4:10












    • I have edited the question with how I pass the values to my listView. Please check and let me know.
      – Zac
      Nov 6 at 4:17


















    up vote
    -1
    down vote













    I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:



    Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
    Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.



    ArrayList<String> row1 = new ArrayList<>();
    ArrayList<String> row2 = new ArrayList<>();
    ArrayList<String> row3 = new ArrayList<>();
    ArrayList<String> row4 = new ArrayList<>();
    for(int i = 1; i<= hashMap.size(); i++){
    if(i%4 == 1){
    int keynum = i - 1;
    keynum = keynum/4;
    String st = "key" + Integer.toString(keynum);
    //Assuming that the values are integers,
    row1.add(st +" " + hashMap.get(st));
    }else if(i%4 == 2){
    int keynum = i - 2;
    keynum = keynum/4;
    String st = "key" + Integer.toString(keynum);
    row2.add(st +" " + hashMap.get(st));
    }else if(i%4 == 3){
    int keynum = i - 3;
    keynum = keynum/4;
    String st = "key" + Integer.toString(keynum);
    row3.add(st +" " + hashMap.get(st));
    }else if(i%4 == 0){
    int keynum = i;
    keynum = keynum/4;
    String st = "key" + Integer.toString(keynum);
    row4.add(st +" " + hashMap.get(st));
    }
    }


    Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.



    I hope this helps.






    share|improve this answer










    New contributor




    Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
      – Zac
      Nov 5 at 3:39










    • Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
      – Ishaan
      Nov 5 at 13:01













    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%2f53147453%2fhow-to-iterate-through-a-hashmap-to-get-every-fourth-key-value-pair%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.



    Multimap myList = ArrayListMultimap.create();



                myList.put("key","value1");
    myList.put("key","value2");
    myList.put("key","value3");
    myList.put("key","value4");
    myList.put("key1","value5");
    myList.put("key1","value6");
    myList.put("key1","value7");
    myList.put("key1","value8");
    myList.put("key2","value9");
    myList.put("key2","value10");
    myList.put("key2","value11");
    myList.put("key2","value12");
    myList.put("key3","value13");
    myList.put("key3","value14");
    myList.put("key3","value15");
    myList.put("key3","value16");


    Then get all keys from list



      Set myKeySet = myList.keySet();
    Object keys = myKeySet.toArray();


    Now you have set of all key. then



        HashMap<String, String> listHashMap1 = new HashMap<>();
    HashMap<String, String> listHashMap2 = new HashMap<>();
    HashMap<String, String> listHashMap3 = new HashMap<>();
    HashMap<String, String> listHashMap4 = new HashMap<>();


    Now add the value to every list



      for(int i=0;i<4;i++){
    List<String> keyval = (List)myList.get((String)keys[i]);
    listHashMap1.put((String)keys[i],keyval.get(0));
    listHashMap2.put((String)keys[i],keyval.get(1));
    listHashMap3.put((String)keys[i],keyval.get(2));
    listHashMap4.put((String)keys[i],keyval.get(3));
    }


    Now you have set of four row list. This Multimap is using google guava.
    So you need to implement guava in your gradle as below:
    implementation 'com.google.guava:guava:26.0-jre'






    share|improve this answer

























      up vote
      0
      down vote













      Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.



      Multimap myList = ArrayListMultimap.create();



                  myList.put("key","value1");
      myList.put("key","value2");
      myList.put("key","value3");
      myList.put("key","value4");
      myList.put("key1","value5");
      myList.put("key1","value6");
      myList.put("key1","value7");
      myList.put("key1","value8");
      myList.put("key2","value9");
      myList.put("key2","value10");
      myList.put("key2","value11");
      myList.put("key2","value12");
      myList.put("key3","value13");
      myList.put("key3","value14");
      myList.put("key3","value15");
      myList.put("key3","value16");


      Then get all keys from list



        Set myKeySet = myList.keySet();
      Object keys = myKeySet.toArray();


      Now you have set of all key. then



          HashMap<String, String> listHashMap1 = new HashMap<>();
      HashMap<String, String> listHashMap2 = new HashMap<>();
      HashMap<String, String> listHashMap3 = new HashMap<>();
      HashMap<String, String> listHashMap4 = new HashMap<>();


      Now add the value to every list



        for(int i=0;i<4;i++){
      List<String> keyval = (List)myList.get((String)keys[i]);
      listHashMap1.put((String)keys[i],keyval.get(0));
      listHashMap2.put((String)keys[i],keyval.get(1));
      listHashMap3.put((String)keys[i],keyval.get(2));
      listHashMap4.put((String)keys[i],keyval.get(3));
      }


      Now you have set of four row list. This Multimap is using google guava.
      So you need to implement guava in your gradle as below:
      implementation 'com.google.guava:guava:26.0-jre'






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.



        Multimap myList = ArrayListMultimap.create();



                    myList.put("key","value1");
        myList.put("key","value2");
        myList.put("key","value3");
        myList.put("key","value4");
        myList.put("key1","value5");
        myList.put("key1","value6");
        myList.put("key1","value7");
        myList.put("key1","value8");
        myList.put("key2","value9");
        myList.put("key2","value10");
        myList.put("key2","value11");
        myList.put("key2","value12");
        myList.put("key3","value13");
        myList.put("key3","value14");
        myList.put("key3","value15");
        myList.put("key3","value16");


        Then get all keys from list



          Set myKeySet = myList.keySet();
        Object keys = myKeySet.toArray();


        Now you have set of all key. then



            HashMap<String, String> listHashMap1 = new HashMap<>();
        HashMap<String, String> listHashMap2 = new HashMap<>();
        HashMap<String, String> listHashMap3 = new HashMap<>();
        HashMap<String, String> listHashMap4 = new HashMap<>();


        Now add the value to every list



          for(int i=0;i<4;i++){
        List<String> keyval = (List)myList.get((String)keys[i]);
        listHashMap1.put((String)keys[i],keyval.get(0));
        listHashMap2.put((String)keys[i],keyval.get(1));
        listHashMap3.put((String)keys[i],keyval.get(2));
        listHashMap4.put((String)keys[i],keyval.get(3));
        }


        Now you have set of four row list. This Multimap is using google guava.
        So you need to implement guava in your gradle as below:
        implementation 'com.google.guava:guava:26.0-jre'






        share|improve this answer












        Instead of using HashMap use Multimap. And based on key get the value in Collection object. then from each Collection add one one value on all the list.



        Multimap myList = ArrayListMultimap.create();



                    myList.put("key","value1");
        myList.put("key","value2");
        myList.put("key","value3");
        myList.put("key","value4");
        myList.put("key1","value5");
        myList.put("key1","value6");
        myList.put("key1","value7");
        myList.put("key1","value8");
        myList.put("key2","value9");
        myList.put("key2","value10");
        myList.put("key2","value11");
        myList.put("key2","value12");
        myList.put("key3","value13");
        myList.put("key3","value14");
        myList.put("key3","value15");
        myList.put("key3","value16");


        Then get all keys from list



          Set myKeySet = myList.keySet();
        Object keys = myKeySet.toArray();


        Now you have set of all key. then



            HashMap<String, String> listHashMap1 = new HashMap<>();
        HashMap<String, String> listHashMap2 = new HashMap<>();
        HashMap<String, String> listHashMap3 = new HashMap<>();
        HashMap<String, String> listHashMap4 = new HashMap<>();


        Now add the value to every list



          for(int i=0;i<4;i++){
        List<String> keyval = (List)myList.get((String)keys[i]);
        listHashMap1.put((String)keys[i],keyval.get(0));
        listHashMap2.put((String)keys[i],keyval.get(1));
        listHashMap3.put((String)keys[i],keyval.get(2));
        listHashMap4.put((String)keys[i],keyval.get(3));
        }


        Now you have set of four row list. This Multimap is using google guava.
        So you need to implement guava in your gradle as below:
        implementation 'com.google.guava:guava:26.0-jre'







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 5 at 7:08









        kumud kala

        415




        415
























            up vote
            0
            down vote













            From the code you supplied it seems that the source hashMap is of type Map<String, String>.



            You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:



                Map<String, String> one = new HashMap<>();
            Map<String, String> two = new HashMap<>();
            Map<String, String> three = new HashMap<>();
            Map<String, String> four = new HashMap<>();

            hashMap.entrySet().forEach(entry -> {
            String values = entry.getValue();
            one.put(entry.getKey(), values[0]);
            two.put(entry.getKey(), values[1]);
            three.put(entry.getKey(), values[2]);
            four.put(entry.getKey(), values[3]);
            });


            Note: assuming every key always has exactly four values.






            share|improve this answer





















            • But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
              – Zac
              Nov 6 at 4:10












            • I have edited the question with how I pass the values to my listView. Please check and let me know.
              – Zac
              Nov 6 at 4:17















            up vote
            0
            down vote













            From the code you supplied it seems that the source hashMap is of type Map<String, String>.



            You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:



                Map<String, String> one = new HashMap<>();
            Map<String, String> two = new HashMap<>();
            Map<String, String> three = new HashMap<>();
            Map<String, String> four = new HashMap<>();

            hashMap.entrySet().forEach(entry -> {
            String values = entry.getValue();
            one.put(entry.getKey(), values[0]);
            two.put(entry.getKey(), values[1]);
            three.put(entry.getKey(), values[2]);
            four.put(entry.getKey(), values[3]);
            });


            Note: assuming every key always has exactly four values.






            share|improve this answer





















            • But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
              – Zac
              Nov 6 at 4:10












            • I have edited the question with how I pass the values to my listView. Please check and let me know.
              – Zac
              Nov 6 at 4:17













            up vote
            0
            down vote










            up vote
            0
            down vote









            From the code you supplied it seems that the source hashMap is of type Map<String, String>.



            You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:



                Map<String, String> one = new HashMap<>();
            Map<String, String> two = new HashMap<>();
            Map<String, String> three = new HashMap<>();
            Map<String, String> four = new HashMap<>();

            hashMap.entrySet().forEach(entry -> {
            String values = entry.getValue();
            one.put(entry.getKey(), values[0]);
            two.put(entry.getKey(), values[1]);
            three.put(entry.getKey(), values[2]);
            four.put(entry.getKey(), values[3]);
            });


            Note: assuming every key always has exactly four values.






            share|improve this answer












            From the code you supplied it seems that the source hashMap is of type Map<String, String>.



            You could iterate over the entries of the map, and for each entry store the four values in different result containers. Since you indicated the result containers should be maps as well:



                Map<String, String> one = new HashMap<>();
            Map<String, String> two = new HashMap<>();
            Map<String, String> three = new HashMap<>();
            Map<String, String> four = new HashMap<>();

            hashMap.entrySet().forEach(entry -> {
            String values = entry.getValue();
            one.put(entry.getKey(), values[0]);
            two.put(entry.getKey(), values[1]);
            three.put(entry.getKey(), values[2]);
            four.put(entry.getKey(), values[3]);
            });


            Note: assuming every key always has exactly four values.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 5 at 13:34









            Boris van Katwijk

            8471821




            8471821












            • But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
              – Zac
              Nov 6 at 4:10












            • I have edited the question with how I pass the values to my listView. Please check and let me know.
              – Zac
              Nov 6 at 4:17


















            • But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
              – Zac
              Nov 6 at 4:10












            • I have edited the question with how I pass the values to my listView. Please check and let me know.
              – Zac
              Nov 6 at 4:17
















            But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
            – Zac
            Nov 6 at 4:10






            But how do I pass only four values to the TA link on the Simple list adapter, and multiple IA and XA values under each TA?
            – Zac
            Nov 6 at 4:10














            I have edited the question with how I pass the values to my listView. Please check and let me know.
            – Zac
            Nov 6 at 4:17




            I have edited the question with how I pass the values to my listView. Please check and let me know.
            – Zac
            Nov 6 at 4:17










            up vote
            -1
            down vote













            I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:



            Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
            Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.



            ArrayList<String> row1 = new ArrayList<>();
            ArrayList<String> row2 = new ArrayList<>();
            ArrayList<String> row3 = new ArrayList<>();
            ArrayList<String> row4 = new ArrayList<>();
            for(int i = 1; i<= hashMap.size(); i++){
            if(i%4 == 1){
            int keynum = i - 1;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            //Assuming that the values are integers,
            row1.add(st +" " + hashMap.get(st));
            }else if(i%4 == 2){
            int keynum = i - 2;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row2.add(st +" " + hashMap.get(st));
            }else if(i%4 == 3){
            int keynum = i - 3;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row3.add(st +" " + hashMap.get(st));
            }else if(i%4 == 0){
            int keynum = i;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row4.add(st +" " + hashMap.get(st));
            }
            }


            Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.



            I hope this helps.






            share|improve this answer










            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
              – Zac
              Nov 5 at 3:39










            • Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
              – Ishaan
              Nov 5 at 13:01

















            up vote
            -1
            down vote













            I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:



            Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
            Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.



            ArrayList<String> row1 = new ArrayList<>();
            ArrayList<String> row2 = new ArrayList<>();
            ArrayList<String> row3 = new ArrayList<>();
            ArrayList<String> row4 = new ArrayList<>();
            for(int i = 1; i<= hashMap.size(); i++){
            if(i%4 == 1){
            int keynum = i - 1;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            //Assuming that the values are integers,
            row1.add(st +" " + hashMap.get(st));
            }else if(i%4 == 2){
            int keynum = i - 2;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row2.add(st +" " + hashMap.get(st));
            }else if(i%4 == 3){
            int keynum = i - 3;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row3.add(st +" " + hashMap.get(st));
            }else if(i%4 == 0){
            int keynum = i;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row4.add(st +" " + hashMap.get(st));
            }
            }


            Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.



            I hope this helps.






            share|improve this answer










            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















            • Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
              – Zac
              Nov 5 at 3:39










            • Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
              – Ishaan
              Nov 5 at 13:01















            up vote
            -1
            down vote










            up vote
            -1
            down vote









            I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:



            Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
            Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.



            ArrayList<String> row1 = new ArrayList<>();
            ArrayList<String> row2 = new ArrayList<>();
            ArrayList<String> row3 = new ArrayList<>();
            ArrayList<String> row4 = new ArrayList<>();
            for(int i = 1; i<= hashMap.size(); i++){
            if(i%4 == 1){
            int keynum = i - 1;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            //Assuming that the values are integers,
            row1.add(st +" " + hashMap.get(st));
            }else if(i%4 == 2){
            int keynum = i - 2;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row2.add(st +" " + hashMap.get(st));
            }else if(i%4 == 3){
            int keynum = i - 3;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row3.add(st +" " + hashMap.get(st));
            }else if(i%4 == 0){
            int keynum = i;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row4.add(st +" " + hashMap.get(st));
            }
            }


            Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.



            I hope this helps.






            share|improve this answer










            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            I think this may work. I didn't get to test it but what it should do is there is an arraylist for each row and each element in the arraylist will contain "key value, " So for example:



            Row 1 ArrayList: {"key value1", "key1 value5", "key2 value9", "key3 value13", "..."}
            Please note that I didn't know what your hashMap would contain so the code below assumes that you have already initialized it and added in values.



            ArrayList<String> row1 = new ArrayList<>();
            ArrayList<String> row2 = new ArrayList<>();
            ArrayList<String> row3 = new ArrayList<>();
            ArrayList<String> row4 = new ArrayList<>();
            for(int i = 1; i<= hashMap.size(); i++){
            if(i%4 == 1){
            int keynum = i - 1;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            //Assuming that the values are integers,
            row1.add(st +" " + hashMap.get(st));
            }else if(i%4 == 2){
            int keynum = i - 2;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row2.add(st +" " + hashMap.get(st));
            }else if(i%4 == 3){
            int keynum = i - 3;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row3.add(st +" " + hashMap.get(st));
            }else if(i%4 == 0){
            int keynum = i;
            keynum = keynum/4;
            String st = "key" + Integer.toString(keynum);
            row4.add(st +" " + hashMap.get(st));
            }
            }


            Now that you have the ArrayLists, all you have to do is pass them into an ArrayAdapter. If you need help with passing ArrayLists into your own custom listview adapter, you can read this tutorial here on how to do that.



            I hope this helps.







            share|improve this answer










            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited Nov 5 at 13:00





















            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 5 at 2:33









            Ishaan

            647




            647




            New contributor




            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Ishaan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
              – Zac
              Nov 5 at 3:39










            • Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
              – Ishaan
              Nov 5 at 13:01




















            • Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
              – Zac
              Nov 5 at 3:39










            • Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
              – Ishaan
              Nov 5 at 13:01


















            Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
            – Zac
            Nov 5 at 3:39




            Hi, the question is about how to add these values in 4 rows in the listview via the variables TA, IA and XA as shown in the question - with multiple IA and XA values per TA. Thanks.
            – Zac
            Nov 5 at 3:39












            Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
            – Ishaan
            Nov 5 at 13:01






            Yes, so I made changes to my answer above. The four ArrayLists contain key value2 key1 value6 key2 value10 key3 value14 like you wanted. There is one arrayList per row. Now, what you need to do is create an arrayList of those arrayLists which can be found at the link here. Once you have that single ArrayList, just pass it on to your own ListView adapter. [this tutorial here] (medium.com/mindorks/…) explains how to work with custom adapters.
            – Ishaan
            Nov 5 at 13:01




















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147453%2fhow-to-iterate-through-a-hashmap-to-get-every-fourth-key-value-pair%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini