jai
jai

Reputation: 305

how to iterate a list like List<Map<String,Object>>

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

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

Upvotes: 17

Views: 145057

Answers (7)

JesseBoyd
JesseBoyd

Reputation: 1106

This is an easy way to iterate over a list of Maps as my starting point. My List had one Map object inside with 3 values

List<Map<String, Object>>

using Java's functional programming in a rather short and succinct manner. The purpose here was to pull out all the maps stored in a list and print them out. I could have also collected the values etc.

answerListOfMaps.stream().map(map -> map.entrySet())
        .forEach(System.out::println );

output in Eclipse IDE console looked like this:

[isAllowed=true, isValid=true, cardNumber=672xxxxxxxxxxx]

x's for Obfuscation

alternate way:

answerListOfMaps.stream().flatMap(map -> map.entrySet().stream())
        .forEach( entry -> System.out.println(entry.getKey() + ":" + entry.getValue()) );

console:

isAllowed:true
isValid:true
cardNumber:672xxxxxxxxxxx

Upvotes: 1

Yanish Pradhananga
Yanish Pradhananga

Reputation: 755

I'm posting you one simple Example of List<Map<String,Object>>.

    public static void main(String[] args){

    Map<String,Object> map1 = new HashMap<>();
    map1.put("Map1 Key1", (Object) "Map1 value1");
    map1.put("Map1 Key2", (Object) "Map1 value2");

    Map<String,Object> map2 = new HashMap<>();
    map2.put("Map2 Key1", (Object) "Map2 value1");
    map2.put("Map2 Key2", (Object) "Map2 value2");

    List<Map<String,Object>> list = new ArrayList<>();
    list.add(map1);
    list.add(map2);

    list.stream().forEach(mapsData->{
        mapsData.entrySet().forEach(mapData->{
            System.out.println("Key:"+mapData.getKey()+ " Value:"+mapData.getValue());
        });
    });
}

Upvotes: 5

Lova Chittumuri
Lova Chittumuri

Reputation: 3313

    Map<String, String> map = new HashMap<>();
    map.put("First", "1");
    map.put("Second", "2");
    map.put("third", "3");
    map.put("four", "4");

    // here is the logic 
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " - " + entry.getValue());
    }

Upvotes: 2

Am1rr3zA
Am1rr3zA

Reputation: 7421

with java 1.8 (8) you can re-write it like:

list.forEach(item -> 
                item.forEach((k, v) -> System.out.println(k + ": " + (String)v)
    );

Upvotes: 6

WhiteFang34
WhiteFang34

Reputation: 72049

It sounds like you're looking for something like this:

List<Map<String, Object>> list; // this is what you have already

for (Map<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
    }
}

Upvotes: 36

dertkw
dertkw

Reputation: 7848

List<Map<String, Object>> list = getMyMap();    
for (Map<String, Object> map : list) {
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        System.out.println(entry.getKey() + " - " + entry.getValue());
    }
}
  1. Loop through list of maps
  2. Handle map entries

Upvotes: 10

npinti
npinti

Reputation: 52185

This should work:

List<Map<String, Object>> list = ...

for (Map<String, Object> map : list)
{
     ...
}

You can also use an iterator or the get method within a for loop to access the elements within the List.

Upvotes: 2

Related Questions