简体   繁体   中英

Iterate over List<Map<String, String>> in Java

I'm trying to iterate List<Map<String, String>> in Java. However, I'm not able to iterate it properly. Can any one guide me?

Iterator it = list.iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    System.out.println(pairs.getKey() + " = " + pairs.getValue());
}

Thanks,

public static void main(String[] args) {
        List<Map<String, String>> myListOfMaps = new ArrayList<Map<String, String>>();
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("Fname", "Ankur");

        Map<String, String> map2 = new HashMap<String, String>();
        map2.put("Lname", "Singhal");

        myListOfMaps.add(map1);
        myListOfMaps.add(map2);

        for (int i = 0 ; i < myListOfMaps.size() ; i++) {
            Map<String, String> myMap = myListOfMaps.get(i);
            System.out.println("Data For Map" + i);
            for (Entry<String, String> entrySet : myMap.entrySet()) {
                System.out.println("Key = " + entrySet.getKey() + " , Value = " + entrySet.getValue());
            }
        }
    }

output

Data For Map0
Key = Fname , Value = Ankur
Data For Map1
Key = Lname , Value = Singhal

Forget using the iterator directly, why not simply this:

List<Map<String,String>> l = new ArrayList<>();
...
// add map elements to list
...

for (Map<String,String> m:l) {
  for (Map.Entry<String,String> e:m.entrySet()) {
    String key = e.getKey();
    String value = e.getValue();
    // Do something with key/value
  }
}

This is called an Enhanced for Loop . Internally it will handle it as a for loop traversing the iterator of any collection, or any other implementation of the Iterable Interface.

It was already used for traversing the Map Entries in one answer, so why not for the list of maps?

Of course, for nested collections, you also need to know how to nest your for-loops (how you put one for loop inside the other).

You iterate over a list with elements of type Map<String, String> . So casting to Map.Entry will give you a ClassCastException .

Try it like this

Iterator it = list.iterator();
while (it.hasNext()) {
    Map<String, String> map = (Map<String, String>) it.next();
    for (Map.Entry entry : map.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
}

It would be easier for you, if you didn't use the raw types Iterator and Map.Entry . Use generics wherever possible. So the code would look like this:

Iterator<Map<String, String>> it = list.iterator();
while (it.hasNext()) {
    Map<String, String> map = it.next(); //so here you don't need a potentially unsafe cast
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " = " + entry.getValue());
    }
}

list has no entySet() method!

Try this:

    final Iterator<Map<String, String>> it = list.iterator();
    while (it.hasNext())
    {
        Map<String, String> mapElement = it.next();
        // do what you want with the mapElement
    }

Of course, you will need another loop to iterate over the elements in the map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM