8

I am trying to concat list of a stream and process it.

class A {
    public List<B> bList;
}
List<A> aList;
aList.stream().map(a -> a.bList)....

Here i get several list of b.

But, I would like to collect all my b in only one list. Any ideas ?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Clem
  • 2,150
  • 15
  • 17

1 Answers1

18

That's what flatMap is for :

List<B> bList = aList.stream()
                     .flatMap(a -> a.bList.stream())
                     .collect(Collectors.toList());
Eran
  • 387,369
  • 54
  • 702
  • 768