Flatmap
is applicable for anything that is a “container of something”
Stream and CompletableFuture
Combination of mapping then flattening
The idea for map
take all elements of a stream or collection and apply a function to
Applying map to create a list of lists
For every singular element, we’re creating a plural. If you perform a map with a plural
Instead use flatmap
Example
We are given only a Stream and our goal is to determine all the salaries of all employees, including Managers and their Employees.
Instead of using forEach and start digging through those salaries.
This, unfortunately, would model our code to the structure of the data and would cause needless complexity
Use flatmap
Last updated
Was this helpful?