Using flatMapping and filtering
You’ve used the filter and flatMap methods on the Stream before. The ing ending method names are used within the collect call instead of directly on the Stream.
When we’re in the middle of the reduce operation, collecting data, if we have to deal with a one-to-many function, we can use flatMapping instead of mapping the same way we can choose flatMap instead of map at the stream processing level—see When to Use map vs. flatMap.
Also, if we’re right in the middle of performing a reduce operation, and we decide to discard some values that are either not visible at the stream level or rather inconvenient to deal with there, we can use the filtering operation to tailor the collect operation.
You can use the two ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access