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 ?

0

1 Answer 1

18

That's what flatMap is for :

List<B> bList = aList.stream()
                     .flatMap(a -> a.bList.stream())
                     .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.