【发布时间】:2016-07-21 23:54:07
【问题描述】:
如果我想计算名册中具有特定姓名的人数:
我应该得到类似[Bob:2, Joe:1, Mary:1, Kane: 1]
List<Person> names = Arrays.toList(Bob, Joe, Bob, Mary, Kane);
Map<String, List<int>> = names.stream().collect(
Collectors.groupingBy(
Person::getName,
Collectors.reducing(
0,
//Is there a way here I can get the count of the names from the grouping by above this?
Integer::sum
)
)
)
【问题讨论】:
-
Collectors.groupingBy(Person::getName, Collectors.counting()). -
Collectors.summingInt(e->1)可以胜任,但Collectors.counting()更好。 -
评论不是为了回答?