JAVA8 - Grouping with lambda

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

+4
source share
3 answers

, :

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())));

rrrList.stream().flatMap(x -> x.xySets.entrySet().stream()), . , :

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; }
}
+2

- ?:

    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);
        });
    });
+5

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())));
+1
source

All Articles