Iterate over ArrayList of ArrayList of Map

FuryMaxim :

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);
            }
        }
    }
Jainik :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152705&siteId=1