Java Stream Collectors.toMap value is a Set

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

    Same Same But Different

    Map> m = new HashMap<>();
    
    as.forEach(a -> {
        m.computeIfAbsent(a.name, v -> new HashSet<>())
                .add(a.property);
        });
    

提交回复
热议问题