Java Stream Collectors.toMap value is a Set

前端 未结 4 1155
旧巷少年郎
旧巷少年郎 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:09

    Also, you can use the merger function option of the Collectors.toMap function Collectors.toMap(keyMapper,valueMapper,mergeFunction) as follows:

    final Map m = as.stream()
                                    .collect(Collectors.toMap(
                                              x -> x.name, 
                                              x -> x.property,
                                              (property1, property2) -> property1+";"+property2);
    

提交回复
热议问题