[英]JAVA8 - Grouping with lambda
I have a collection with structure like this: 我有一个像这样结构的集合:
@Entity
public class RRR{
private Map<XClas, YClas> xySets;
}
and XClas
has a field called ZZZ
和
XClas
有一个名为ZZZ
的领域
my question is: I would like to aggregate it with lambda to get a Map<ZZZ, List<RRR>>
. 我的问题是:我想将它与lambda聚合得到一个
Map<ZZZ, List<RRR>>
。
Is it possible? 可能吗? Now I'm stuck with:
现在我坚持:
Map xxx = rrrList.stream().collect(
Collectors.groupingBy(x->x.xySets().entrySet().stream().collect(
Collectors.groupingBy(y->y.getKey().getZZZ()))));
but it's Map<Map<ZZZ, List<XClas>>, List<RRR>>
so it's not what I was looking for :) 但它是
Map<Map<ZZZ, List<XClas>>, List<RRR>>
所以它不是我想要的:)
Right now just to make it work, I did aggregation with two nested loops, but it would be so great, if you could help me make it done with lambdas. 现在只是为了让它工作,我用两个嵌套循环进行聚合,但如果你可以帮我用lambdas完成它,那就太棒了。
EDIT 编辑
I post what I got by now, as asked. 据我所知,我发布了现在的内容。 I already left nested loops, and I manage to work my way up to this point:
我已经离开了嵌套循环,并且我设法工作到这一点:
Map<ZZZ, List<RRR>> temp;
rrrList.stream().forEach(x -> x.getxySetsAsList().stream().forEach(z -> {
if (temp.containsKey(z.getKey().getZZZ())){
List<RRR> uuu = new LinkedList<>(temp.get(z.getKey().getZZZ()));
uuu.add(x);
temp.put(z.getKey().getZZZ(), uuu);
} else {
temp.put(z.getKey().getZZZ(), Collections.singletonList(x));
}
}));
Thanks in advance 提前致谢
Something like that? 那样的东西? :
:
Map<ZZZ, List<RRR>> map = new HashMap<>();
list.stream().forEach(rrr -> {
rrr.xySets.keySet().stream().forEach(xclas -> {
if (!map.containsKey(xclas.zzz))
map.put(xclas.zzz, new ArrayList<RRR>());
map.get(xclas.zzz).add(rrr);
});
});
Another way you could do this: 另一种方法可以做到这一点:
Map<Z, List<R>> map = rs.stream()
.map(r -> r.xys.keySet()
.stream()
.collect(Collectors.<X, Z, R>toMap(x -> x.z, x -> r, (a, b) -> a)))
.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(Entry::getKey,
Collectors.mapping(Entry::getValue, Collectors.toList())));
I have tried around a bit and found the following solution, posting it here just as another example: 我尝试了一下,找到了以下解决方案,将其发布在这里作为另一个例子:
rrrList.stream().map(x -> x.xySets).map(Map::entrySet).flatMap(x -> x.stream())
.collect(Collectors.groupingBy(x -> x.getKey().getZZZ(),
Collectors.mapping(Entry::getValue, Collectors.toList())));
The first line could also be written as rrrList.stream().flatMap(x -> x.xySets.entrySet().stream())
which might be found more readable. 第一行也可以写成
rrrList.stream().flatMap(x -> x.xySets.entrySet().stream())
,它们可能更易读。 Here is self-contained example code for those wanting to play around themselves: 这是一个自包含的示例代码,适合那些想要自己玩耍的人:
public static void main(String[] args) {
List<RRR> rrrList = Arrays.asList(new RRR(), new RRR(), new RRR());
System.out.println(rrrList);
Stream<Entry<XClas, YClas>> sf = rrrList.stream().map(x -> x.xySets).map(Map::entrySet).flatMap(x -> x.stream());
Map<ZZZ, List<YClas>> res = sf.collect(Collectors.groupingBy(x -> x.getKey().getZZZ(), Collectors.mapping(Entry::getValue, Collectors.toList())));
System.out.println(res);
}
public static class RRR {
static XClas shared = new XClas();
private Map<XClas, YClas> xySets = new HashMap<>();
RRR() { xySets.put(shared, new YClas()); xySets.put(new XClas(), new YClas()); }
static int s = 0; int n = s++;
public String toString() { return "RRR" + n + "(" + xySets + ")"; }
}
public static class XClas {
private ZZZ zzz = new ZZZ();
public ZZZ getZZZ() { return zzz; }
public String toString() { return "XClas(" + zzz + ")"; }
public boolean equals(Object o) { return (o instanceof XClas) && ((XClas)o).zzz.equals(zzz); }
public int hashCode() { return zzz.hashCode(); }
}
public static class YClas {
static int s = 0; int n = s++;
public String toString() { return "YClas" + n; }
}
public static class ZZZ {
static int s = 0; int n = s++ / 2;
public String toString() { return "ZZZ" + n; }
public boolean equals(Object o) { return (o instanceof ZZZ) && ((ZZZ)o).n == n; }
public int hashCode() { return n; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.