Java Stream converter Map to List
I have this object filled in:
Map<String, List<LogLine>> logMap = new TreeMap<>();
And after making a filter I would like a flat list of it, but I can only create List List
List<List<LogLine>> foo = logMap.entrySet().stream()
.filter(map -> map.getValue().size() > parameters.getThreshold())
.map(map -> map.getValue())
.collect(Collectors.toList());
How can I create only one list with all loglines using stream? I tried to use flatMap, but the compiler does not let.
0
1 answers
I believe the operator you want is flatMap
:
List<LogLine> foo = logMap.entrySet().stream()
.filter(map -> map.getValue().size() > parameters.getThreshold())
.flatMap(map -> map.getValue().stream())
.collect(Collectors.toList());
It substitues the current stream [logMap.entrySet().stream()
] by a new stream produced by applying a mapper function to each element [map.getValue().stream()
].
1
Author: Leonardo Lima, 2017-12-20 09:25:28