How would I get this data structure using Java 8 API?
This is my object structure:
class A {
B b;
public A(B b) {
this.b = b;
}
}
class B {
List<A> as;
private int i;
public B(int i) {
this.i = i;
}
}
I'm trying to aggregate it to
Map<A, List<B>> bs;
from
List<A> as = new ArrayList<>();
as.add(a1);
as.add(a2);
as.add(a3);
With groupingBy:
Map<B, List<A>> bs = as.stream().collect(Collectors.groupingBy(A::getB));
Assuming class A
has a getB()
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With