Java Stream Collectors.toMap value is a Set

前端 未结 4 1150
旧巷少年郎
旧巷少年郎 2021-02-07 21:29

I want to use a Java Stream to run over a List of POJOs, such as the list List below, and transform it into a Map Map>

4条回答
  •  不思量自难忘°
    2021-02-07 22:02

    groupingBy does exactly what you want:

    import static java.util.stream.Collectors.*;
    ...
    as.stream().collect(groupingBy((x) -> x.name, mapping((x) -> x.property, toSet())));
    

提交回复
热议问题