Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map within List - How to Iterate using Stream

How to Iterate

List<Map<String, String>> 

using streams..i have tried without streams and i was able to iterate.

for (Map<String, String> temp1 : maps){
                    for(Map.Entry<String, String> temp2: temp1.entrySet())

Is there a way to iterate using Streams..

like image 946
JavaLearner1 Avatar asked Feb 22 '26 18:02

JavaLearner1


1 Answers

You can use Stream.flatMap(...) to iterate each map entry from the list of maps.

List<Map<String, String>> maps = ...
maps.stream()
    .flatMap(map -> map.entrySet().stream())
    .forEach(entry -> System.out.println(entry));

From the docs:

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

like image 155
sn42 Avatar answered Feb 25 '26 06:02

sn42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!