Differences between Collectors.toMap() and Collectors.groupingBy() to collect into a Map

前端 未结 4 1538
傲寒
傲寒 2021-02-01 16:10

I want to create a Map from a List of Points and have inside the map all entries from the list mapped with the same parentId such as

4条回答
  •  不思量自难忘°
    2021-02-01 16:39

    The following code does the stuff. Collectors.toList() is the default one, so you can skip it, but in case you want to have Map> Collectors.toSet() would be needed.

    Map> map = pointList.stream()
                    .collect(Collectors.groupingBy(Point::getParentId, Collectors.toList()));
    

提交回复
热议问题