如何在 ImmutableMap 中扁平化 ImmutableList

比如说,我有一个名为 DomainObject 的类,


class DomainObject {


  private Long id;

  private String domainParam;

}

我收到的对象列表如下:


(id, domainType) = (1, "A") , (1, "B"), (3, "C"), (4, "A"), (1, "C")

毕竟,我想接收带有 Key(ImmutableList of Id) 和 Pair(Immutable list of domainParam) 的 ImmutableMap,例如:


1 [A, B, C]

3 [C]

4 [A]

现在我收到类似的东西:


{[1]=[DomainObject(id=1, domainParam=A), DomainObject(id=1, domainParam=B), DomainObject(id=1, domainParam=B)]}

这不是理想的解决方案。


到目前为止,我有这样的代码:



ImmutableMap<ImmutableList<Long>, ImmutableList<DomainObject>> groupedDomainObject(

      List<DomainObject> domainObjectList) {


    return domainObjectList.stream()

        .collect(

            Collectors.collectingAndThen(

                Collectors.groupingBy(

                    (domainObject) -> ImmutableList.of(domainObject.getId()),

                    ImmutableList.<DomainObject>toImmutableList()),

                ImmutableMap::copyOf));

}

我即将实现一个目标,但我如何才能从这部分中获得价值:


ImmutableList.<DomainObject>toImmutableList()

接收唯一没有 DomainObject id 的 domainParam。


如果我能得到任何帮助,我将不胜感激。


湖上湖
浏览 115回答 2
2回答

慕尼黑5688855

&nbsp; &nbsp; &nbsp; &nbsp;......&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.collectingAndThen(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x -> ImmutableList.of(x.getId()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.mapping(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DomainObject::getDomainParam,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImmutableList.toImmutableList())),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImmutableMap::copyOf&nbsp; &nbsp; &nbsp; &nbsp; ));

拉丁的传说

ImmutableMap<Long, ImmutableList<String>> groupedDomainObject(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<DomainObject> domainObjectList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return domainObjectList&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.collectingAndThen(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.toMap(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DomainObject::getId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj -> ImmutableList.of(obj.domainParam),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (a, b) -> ImmutableList.<String>builder().addAll(a).addAll(b).build()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ImmutableMap::copyOf&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java