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

前端 未结 4 1537
傲寒
傲寒 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:55

    Collectors.groupingBy is exactly what you want, it creates a Map from your input collection, creating an Entry using the Function you provide for it's key, and a List of Points with your associated key as it's value.

    Map> pointByParentId = chargePoints.stream()
        .collect(Collectors.groupingBy(Point::getParentId));
    

提交回复
热议问题