Difference Between map and flatMap in Java 8
Definition in Java 8 Stream and Optional
Optional
- map: If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
- flatMap: If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. This Method is similar to map(Function), but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.