Java Stream convert MapString, Obj to ListObj

0

I have this object filled:

    Map<String, List<LogLine>> logMap = new TreeMap<>();

And after making a filter, I'd 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 using flatMap, but the compiler does not.

    
asked by anonymous 20.12.2017 / 09:00

1 answer

1

I think 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 substitutes the current stream [ logMap.entrySet().stream() ] for a new stream produced by applying a mapping function to each element [ map.getValue().stream() ].

    
20.12.2017 / 10:25