You can achieve this with flat mapping all elements of a stream into your stream.
Let me explain by this code:
groups.stream()
.flatMap(group -> group.getPersons().stream());
What you do here, is:
- Obtain a
Stream<Collection<Group>>
. - Then flat map every obtained
Stream<Person>
, from aGroup
, back into your original stream, which is of typeStream<Person>
.
Now after the flatMap()
, you can do whatever you want with the obtained Stream<Person>
.