Jacky Coogan Jacky Coogan - 4 months ago 27
Java Question

Java - How to iterate over a hashmap of list of hashmap

I want to iterate over a hashmap of list of hashmap.

For Example,

HashMap<String, List<HashMap<String, String>>>

How do I iterate over this map.

Please help me to find a solution...


You should probably look over your design, this sounds like something that could be structured in an easier way. Consider maybe breaking this collection up into classes. But, in case you cannot change it now (or don't have the time), I'll offer a hand.

The easiest way is probably to break the iteration up in several steps:

  1. Iterate over the inner HashMaps
  2. Iterate over the lists
  3. Iterate over the content in the inner HashMaps

Here's the basic code:

for( List<HashMap<String, String>> list : outer.values() ) {
    for( HashMap<String, String> map : list ) {
        for( String value : map.values() ) {
            //Do something