-2

I have a method which is returning Map<String, List<Map<String, Object>>>

How to iterate over a map like Map<String, List<Map<String, Object>>>

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
Sai Kishore Mani
  • 49
  • 1
  • 3
  • 11

1 Answers1

4

You would iterate over it like you would iterate over every other map. You just need to "recursively" iterate over the List and another Aap inside the iteration loop.

One possible way (without functional code):

Map<String, List<Map<String, Object>>> yourMap = ...;
//iterate over outer map
for(Map.Entry<String, List<Map<String, Object>>> topEntry : yourMap.entrySet()) {
  String topKey = topEntry.getKey();
  //iterate over list
  for(Map<String, Object> innerMap : topEntry.getValue()) {
    //iterate over inner map
    for(Map.Entry<String, Object> innerEntry : innerMap.entrySet()) {
      String innerKey = innerEntry.getKey();
      Object innerValue = innerEntry.getValue();
    }
  }
}

You can also switch the foreach loop iterating over the List to a fori loop [ for(int i=0; i<list.size(); i++) ] if you need to know the list-index.

Michael Ritter
  • 580
  • 2
  • 9