-6

I'm having problems working out how to iterate this structure.

ArrayList<HashMap<String, HashMap<String, String>>> list

Can you help me work out how to walk the complete structure?

Here's what I've got to so far but where do I go next? I need to get all the way down to the deepest data.

for (HashMap<String, String> map : data)
     for (Entry<String, String> entry : map.entrySet())
Spartan
  • 3,213
  • 6
  • 26
  • 31

1 Answers1

3

How about this?

    ArrayList<HashMap<String, HashMap<String, String>>> list = new ArrayList<>();

    for (HashMap<String, HashMap<String, String>> m : list) {
        for (Map.Entry<String, HashMap<String, String>> e : m.entrySet()) {
            String key1 = e.getKey();
            for (Map.Entry<String, String> me : e.getValue().entrySet()) {
                String key2 = me.getKey();
                String value = me.getValue();
            }
        }
    }

Note that you really should be using the interface form of the objects:

    List<Map<String, Map<String, String>>> list = new ArrayList<>();

    for (Map<String, Map<String, String>> m : list) {
        // All Maps.
        for (Map.Entry<String, Map<String, String>> e : m.entrySet()) {
            // Outer key.
            String key1 = e.getKey();
            for (Map.Entry<String, String> me : e.getValue().entrySet()) {
                // Inner key.
                String key2 = me.getKey();
                // The String value.
                String value = me.getValue();
            }
        }
    }
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213