如何在 ImmutableMap 中扁平化 ImmutableList
How to flat ImmutableList in ImmutableMap
说,我有一个名为 DomainObject 的 class,
class DomainObject {
private Long id;
private String domainParam;
}
我收到的对象列表如下:
(id, domainType) = (1, "A") , (1, "B"), (3, "C"), (4, "A"), (1, "C")
毕竟,我想接收带有 Key(Id 的 ImmutableList)和 Pair(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。
如果能得到任何帮助,我将不胜感激。
......
.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(
x -> ImmutableList.of(x.getId()),
Collectors.mapping(
DomainObject::getDomainParam,
ImmutableList.toImmutableList())),
ImmutableMap::copyOf
));
ImmutableMap<Long, ImmutableList<String>> groupedDomainObject(
List<DomainObject> domainObjectList) {
return domainObjectList
.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toMap(
DomainObject::getId,
obj -> ImmutableList.of(obj.domainParam),
(a, b) -> ImmutableList.<String>builder().addAll(a).addAll(b).build()
),
ImmutableMap::copyOf
)
);
}
说,我有一个名为 DomainObject 的 class,
class DomainObject {
private Long id;
private String domainParam;
}
我收到的对象列表如下:
(id, domainType) = (1, "A") , (1, "B"), (3, "C"), (4, "A"), (1, "C")
毕竟,我想接收带有 Key(Id 的 ImmutableList)和 Pair(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。
如果能得到任何帮助,我将不胜感激。
......
.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(
x -> ImmutableList.of(x.getId()),
Collectors.mapping(
DomainObject::getDomainParam,
ImmutableList.toImmutableList())),
ImmutableMap::copyOf
));
ImmutableMap<Long, ImmutableList<String>> groupedDomainObject(
List<DomainObject> domainObjectList) {
return domainObjectList
.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toMap(
DomainObject::getId,
obj -> ImmutableList.of(obj.domainParam),
(a, b) -> ImmutableList.<String>builder().addAll(a).addAll(b).build()
),
ImmutableMap::copyOf
)
);
}