英文:
java 8 - how to group by every element from the list
问题
我有两个列表:'groups' 和 'people'。最终,我想要得到一个按每个 'group' 分组的映射。
以下代码由于最后一个 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)));
    }
英文:
I have 2 lists: 'groups' and 'people'. In the end I want to get a map grouped by every 'group'.
The code below doesn't compile due to last flatMap method, saying that 'Non-static method cannot be referenced from a static context'.
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)));
   }
</details>
# 答案1
**得分**: 0
`flatMap` 应该在 `collect` 步骤之前使用,以生成所有 `Group` 和 `Person` 的配对。然后您可以对这些配对进行分组。
```java
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))
           .flatMap(p -> p.getGroups()
                          .stream()
                          .map(g -> new SimpleEntry<Group, Person>(g, p)))
           .collect(Collectors.groupingBy(Map.Entry::getKey,
                                          Collectors.mapping(Map.Entry::getValue,
                                                             Collectors.toList())));
}
英文:
flatMap should be used before the collect step, to generate all pairs of Group and Person. Then you can group these pairs.
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))
               .flatMap(p -> p.getGroups()
                              .stream()
                              .map(g -> new SimpleEntry<Group,Person>(g,p)))
               .collect(Collectors.groupingBy(Map.Entry::getKey,
                                              Collectors.mapping(Map.Entry::getValue,
                                                                 Collectors.toList())));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论