I have a collection with a structure like this:
@Entity
public class RRR{
private Map<XClas, YClas> xySets;
}
and XClashas a field calledZZZ
my question is: I would like to combine it with lambda to get Map<ZZZ, List<RRR>>.
Is it possible? Now I'm stuck:
Map xxx = rrrList.stream().collect(
Collectors.groupingBy(x->x.xySets().entrySet().stream().collect(
Collectors.groupingBy(y->y.getKey().getZZZ()))));
but this Map<Map<ZZZ, List<XClas>>, List<RRR>>, so this is not what I was looking for :)
Right now, to make it work, I did aggregation with two nested loops, but that would be so cool if you could help me do this with lambda.
EDIT
I am sending what I have, upon request. I already left the nested loops, and I manage to get my way 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
source
share