Java 8 Streams groupingBy collector
With groupingBy:
Map<B, List<A>> bs = as.stream().collect(Collectors.groupingBy(A::getB));
Assuming class A
has a getB()
method.
It's pretty simple actually (assuming hashCode/equals
is present in B
)
as.stream()
.collect(Collectors.groupingBy(A::getB))