【发布时间】:2020-09-06 10:06:14
【问题描述】:
我有 2 个列表:“组”和“人员”。最后,我想得到一张按每个“组”分组的地图。 由于最后一个 flatMap 方法,下面的代码无法编译,说“不能从静态上下文引用非静态方法”。
private static Map<Group, List<Person>> getAllGroupsOfSelectedPeople(List<Person> people) {
List<Group> groups = getCurrentlyActiveGroups(people);
return people.stream()
.filter(p -> CollectionUtils.isNotEmpty(p.getSocials()))
.filter(p -> p.getGroups().stream().anyMatch(groups::contains))
.collect(Collectors.groupingBy(p -> p.getGroups().stream().flatMap(List::stream)));
}
【问题讨论】:
-
能分享一下 Group 和 Person 的主要方法吗?
标签: java collections java-stream