Iterate over List> in Java

后端 未结 4 1178
盖世英雄少女心
盖世英雄少女心 2020-12-17 08:16

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

Itera         


        
相关标签:
4条回答
  • 2020-12-17 08:37

    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());
        }
    }
    
    0 讨论(0)
  • 2020-12-17 08:41

    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).

    0 讨论(0)
  • 2020-12-17 08:42

    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.

    0 讨论(0)
  • 2020-12-17 08:52
    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
    
    0 讨论(0)
提交回复
热议问题