rails rails - 4 months ago 9
Java Question

java streams - collect elements from map of collections using condition on the values

I have a Map. Let's say

Map<Long, List<MyObj>>


I want to collect all the long values (keys)

when at least one myObj answers

myObj.isEnabled=false


using java stream.

I tried

map.entrySet().stream().filter(entry->entry.getValue().stream().filter(x->!x.isEnabled())).findAny().collect()

Answer
List<Long> keys = map.entrySet()
        .stream()
        .filter(e -> e.getValue().stream().anyMatch(o -> !o.isEnabled()))
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());