Java 流分组(按子类型)

Java 流分组(按子类型),java,java-stream,Java,Java Stream,我有两个实体: class Parent { Integer id; List<Child> children; } class Child { Integer id; Parent parent; } 使用Java8的分组方式,我如何做到这一点 现在我正在使用resultList.stream().collect(groupingBy(Parent::getChildren)),但这会创建映射,这不是我要找的。也许您会发现这里很有用: Map&

我有两个实体:

class Parent {
    Integer id;
    List<Child> children;
}

class Child {

    Integer id;
    Parent parent;
}
使用Java8的分组方式,我如何做到这一点

现在我正在使用
resultList.stream().collect(groupingBy(Parent::getChildren))
,但这会创建
映射,这不是我要找的。

也许您会发现这里很有用:

Map<Integer, List<Parent>> childMap = 
    resultList.stream()
              .flatMap(Parent::getChildren)
              .collect(groupingBy(Child::getId, Collectors.mapping(Child::getParent,                                        
                                                    Collectors.toList())));
Map-childMap=
resultList.stream()
.flatMap(父::getChildren)
.collect(groupingBy)(Child::getId,Collectors.mapping)(Child::getParent,
收藏家;

使用
flatMap
获取孩子。然后为子id和父id创建映射条目。然后按子id对地图条目列表进行分组,并将地图条目组值收集为列表

Map<Integer, List<Parent>> myMap = 
      resultList
        .stream()
        .flatMap(e -> e.getChildren().stream()
                       .map(a -> new SimpleEntry<Integer,Parent>(a.getId(), e)))
        .collect(Collectors.groupingBy(Map.Entry::getKey,
                                       Collectors.mapping(Map.Entry::getValue,
                                                          Collectors.toList())));
Map myMap=
结果列表
.stream()
.flatMap(e->e.getChildren().stream())
.map(a->newsimpleEntry(a.getId(),e)))
.collect(Collectors.groupingBy(Map.Entry::getKey、,
Collectors.mapping(Map.Entry::getValue,
收藏家;
或者使用Child的构造函数进行更简化

Map<Integer, List<Parent>> myMap = 
      resultList
        .stream()
        .flatMap(e -> e.getChildren().stream()
                       .map(a -> new Child(a.getId(), e)))
        .collect(Collectors.groupingBy(Child::getId, Collectors.mapping(Child::getParent,
                                                          Collectors.toList())));
Map myMap=
结果列表
.stream()
.flatMap(e->e.getChildren().stream())
.map(a->新子级(a.getId(),e)))
.collect(Collectors.groupingBy)(Child::getId,Collectors.mapping)(Child::getParent,
收藏家;

如果每个家长的每个孩子都有可用的家长数据,@cs95解决方案更好。

看起来有点复杂。更好地使用循环?@HarshalParekh,因为我认为问题很复杂。@HarshalParekh-如果您没有发布使用循环的解决方案,我认为不需要您的评论。人们可以清楚地看到埃克拉维亚做得多么精细。如果你不能鼓励某人,至少不要让他/她泄气。@ArvindKumarAvinash这是相关的。Eklavya可以指出,使用循环更具可读性。仅仅因为使用streams解决了这个问题,并不意味着它不值得一提。后者没有样本数据就很难回答这个问题。我建议你发布样本数据和预期结果,这样你就可以得到正确的答案。每个孩子都有自己的父母吗?我认为这是一个更好的方法。但有一个陷阱,因为每个家长的每个孩子都有可用的家长数据?我最初的想法是不可用的现有代码不应该编译,你需要一个显式的
映射
收集器作为
groupingBy
的下游来实现这一点。@Naman啊,明白了。如Eklavya的解决方案所示。猴子看,猴子做,哈。。。
Map<Integer, List<Parent>> myMap = 
      resultList
        .stream()
        .flatMap(e -> e.getChildren().stream()
                       .map(a -> new Child(a.getId(), e)))
        .collect(Collectors.groupingBy(Child::getId, Collectors.mapping(Child::getParent,
                                                          Collectors.toList())));