1

I have:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Second {

    public static void main(String[] args) {
        List<Map<String, String>> listOfMap = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", "title" + i);
            map.put("text", "text" + i);
            listOfMap.add(map);
        }

        for(Entry<String, HashMap> entry : listOfMap.entrySet()) {

        }

    }

}

from: Multi array/object with Java same as in PHP

but how can i iterate this? I would like show element with System.out.println, but in my code I have error:

The method entrySet() is undefined for the type List <Map<String,String>>

Then I would like to see the results this way:

title0
text0
title1
text1
title2
text2
...
title9
text9

with System.out.println.

Community
  • 1
  • 1
peksak
  • 59
  • 5

1 Answers1

4

Iterate first over the list and then over each map :

for (Map<String, String> map : listOfMap) {
   for(Entry<String, String> entry : map.entrySet()) {
     System.out.println(entry.getKey());
     System.out.println(entry.getValue());
   }
}
Eran
  • 387,369
  • 54
  • 702
  • 768