java streams-如何使用键上的条件平坦集合映射中的所有值

java streams-如何使用键上的条件平坦集合映射中的所有值,java,java-8,java-stream,Java,Java 8,Java Stream,我有一张地图。比方说 Map<Long, List<MyObj>> 使用java流 我试过了 map.entrySet() .stream() .filter(e->anotherSet(e.getKey())) .flatMap(e.getValue) .collect(Collectors.toList); 但是它甚至不编译您有一些语法错误 这将生成所需的列表: List<MyObj> filteredList =

我有一张地图。比方说

Map<Long, List<MyObj>> 
使用java流

我试过了

map.entrySet()
   .stream()
   .filter(e->anotherSet(e.getKey()))
   .flatMap(e.getValue)
   .collect(Collectors.toList);

但是它甚至不编译

您有一些语法错误

这将生成所需的
列表

List<MyObj> filteredList = 
    map.entrySet()
       .stream()
       .filter(e->anotherSet.contains(e.getKey())) // you forgot contains
       .flatMap(e-> e.getValue().stream()) // flatMap requires a Function that 
                                           // produces a Stream
       .collect(Collectors.toList()); // you forgot ()
List<MyObj> filteredList = 
    map.entrySet()
       .stream()
       .filter(e->anotherSet.contains(e.getKey())) // you forgot contains
       .flatMap(e-> e.getValue().stream()) // flatMap requires a Function that 
                                           // produces a Stream
       .collect(Collectors.toList()); // you forgot ()
MyObj[] filteredArray = 
    map.entrySet()
       .stream()
       .filter(e->anotherSet.contains(e.getKey()))
       .flatMap(e-> e.getValue().stream())
       .toArray(MyObj[]::new);