FuryMaxim
FuryMaxim

Reputation: 65

Iterate over ArrayList of ArrayList of Map

I use SimpleExpandableListAdapter to create ExpandableListView for my application. I want to know better how to work with lists and maps and what they are in practice.

 //collection for elements of a single group;
ArrayList<Map<String, String>> childDataItem;

//general collection for collections of elements
ArrayList<ArrayList<Map<String, String>>> childData;


Map<String, String> m;

I know how to iterate over ArrayList of Maps, it is not a problem for me, but I got stuck.

childData = new ArrayList<>();
    childDataItem = new ArrayList<>();
    for (String phone : phonesHTC) {
        m = new HashMap<>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

    childDataItem = new ArrayList<>();
    for (String phone : phonesSams) {
        m = new HashMap<String, String>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

    // создаем коллекцию элементов для третьей группы
    childDataItem = new ArrayList<>();
    for (String phone : phonesLG) {
        m = new HashMap<String, String>();
        m.put("phoneName", phone);
        childDataItem.add(m);
    }
    childData.add(childDataItem);

And I want to Log what childData contains (<ArrayList<Map<String, String>>), but I don't sure that I did that right. ( 2nd loop is a simple ArrayList of Map iteration)

    for (ArrayList<Map<String, String>> outerEntry : childData) {
       for(Map<String, String> i:outerEntry ) {
           for (String key1 : i.keySet()) {
               String value1 = i.get(key1);
               Log.d("MyLogs", "(childData)value1 = " + value1);
               Log.d("MyLogs", "(childData)key = " + key1);
           }
         }


        for (Map<String, String> innerEntry : childDataItem) {
            for (String key : innerEntry.keySet()) {
                String value = innerEntry.get(key);
                Log.d("MyLogs", "(childDataItem)key = " + key);
                Log.d("MyLogs", "(childDataItem)value = " + value);
            }
        }
    }

Upvotes: 2

Views: 92

Answers (2)

Jainik
Jainik

Reputation: 2452

If you want to log all the elements for childData then there is no need for the last loop, you are already fetching them in the first loop. Please remove below code from the program and it will log all items of childData.

for (Map<String, String> innerEntry : childDataItem) {
    for (String key : innerEntry.keySet()) {
        String value = innerEntry.get(key);
        Log.d("MyLogs", "(childDataItem)key = " + key);
        Log.d("MyLogs", "(childDataItem)value = " + value);
    }
}

Above loop is iterating over childDataItem and you are using the same reference again and again in your code so in this case above loop will contain only most recent map items.

For simplicity, I changed your log statements to sysout and here's the example and output:

    ArrayList<Map<String, String>> childDataItem;
    //general collection for collections of elements
    ArrayList<ArrayList<Map<String, String>>> childData;

    Map<String, String> m;


    childData = new ArrayList<>();
    childDataItem = new ArrayList<>();
        m = new HashMap<>();
        m.put("phoneName", "HTC");
        m.put("phoneName1", "HTC1");
        childDataItem.add(m);
    childData.add(childDataItem);

    childDataItem = new ArrayList<>();
        m = new HashMap<String, String>();
        m.put("phoneName", "Samsung");
        childDataItem.add(m);
    childData.add(childDataItem);

    // создаем коллекцию элементов для третьей группы
    childDataItem = new ArrayList<>();
        m = new HashMap<String, String>();
        m.put("phoneName", "LG");
        childDataItem.add(m);
    childData.add(childDataItem);


    for (ArrayList<Map<String, String>> outerEntry : childData) {
       for(Map<String, String> i:outerEntry ) {
           for (String key1 : i.keySet()) {
               String value1 = i.get(key1);
               System.out.println("MyLogs (childData)value1 = " + value1);
               System.out.println("MyLogs (childData)key = " + key1);
           }
         }
    }

Output

MyLogs (childData)value1 = HTC1
MyLogs (childData)key = phoneName1
MyLogs (childData)value1 = HTC
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = Samsung
MyLogs (childData)key = phoneName
MyLogs (childData)value1 = LG
MyLogs (childData)key = phoneName

Upvotes: 1

Adam
Adam

Reputation: 1754

So as you probably know, an array list is just a sequential store of data objects. And a map is a key-value pair mapping where the key is used as the lookup and must be unique. That is to say in a Map you may have many duplicate values but only one key.

As for iterating over a Map you can use an entry set which makes it a little easier. So if you wanted to iterate over an object of type <ArrayList<Map<String, String>> it would look something like this for your childDataItem class.

for(Map<String, String> map : childDataItem){

  //Take each map we have in the list and iterate over the keys + values
  for(Map.Entry<String, String> entry : map){
    String key = entry.getKey(), value = entry.getValue();
  }

}

And in your other case, the example is the same except you have another layer of array list.

for(List<Map<String, String>> childDataItemList : childData){

  for(Map<String, String> map : childDataItemList){

    //Take each map we have in the list and iterate over the keys + values
    for(Map.Entry<String, String> entry : map){
      String key = entry.getKey(), value = entry.getValue();
    }

  }

}

Upvotes: 1

Related Questions