java 8 – 如何按列表中的每个元素分组

huangapple go评论66阅读模式
英文:

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&lt;Group, List&lt;Person&gt;&gt; getAllGroupsOfSelectedPeople(List&lt;Person&gt; people) {
       List&lt;Group&gt; groups = getCurrentlyActiveGroups(people);
       return people.stream()
               .filter(p -&gt; CollectionUtils.isNotEmpty(p.getSocials()))
               .filter(p -&gt; p.getGroups().stream().anyMatch(groups::contains))
               .collect(Collectors.groupingBy(p -&gt; 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&lt;Group, List&lt;Person&gt;&gt; getAllGroupsOfSelectedPeople(List&lt;Person&gt; people) {
       List&lt;Group&gt; groups = getCurrentlyActiveGroups(people);
       return people.stream()
               .filter(p -&gt; CollectionUtils.isNotEmpty(p.getSocials()))
               .filter(p -&gt; p.getGroups().stream().anyMatch(groups::contains))
               .flatMap(p -&gt; p.getGroups()
                              .stream()
                              .map(g -&gt; new SimpleEntry&lt;Group,Person&gt;(g,p)))
               .collect(Collectors.groupingBy(Map.Entry::getKey,
                                              Collectors.mapping(Map.Entry::getValue,
                                                                 Collectors.toList())));
}

huangapple
  • 本文由 发表于 2020年9月6日 18:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63762964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定