10- Map vs flatMap
Map Vs FlatMap
Both map() and flatMap() are originated from Functional Programming. These are the functional operations.
Since Java 8, you can find them in java.util.Optional class and java.util.stream.Stream interface.
In Java, the Stream interface has a map() and flatmap() methods and both have intermediate stream operation and return another stream as method output.
1- map() function:
map() function produces one output for one input value,
OR
The map always transforms into a single value, i.e., establishes a one-to-one mapping between the input and output value.
2- flatMap():
whereas flatMap() function produces an arbitrary no of values as output (ie zero or more than zero) for each input value.
OR
flatMap is used to map and flatten the nested collection or stream. flatMap is a combination of transformation and flattening. Hence, it uses one-to-many mapping.
Difference between map and flatMap in Java
The main difference between the map and flatMap is the return type.
The map produces one output value for each input value where flatMap provides one or more values for each input value. To put it in simple words, a map is for transformation while flatMap is a combination of transformation and flattering.
flatMap() = map() + Flattening.
Note: Flattening is the process of converting several collections of items present inside another collection into a single collection of items. For example, [[1, 2], 3, [4, 5], []] is flattened to [1, 2, 3, 4, 5].
map() can be used where we have to map the elements of a particular collection to a certain function, and then we need to return the stream which contains the updated results.
Example: Multiplying All the elements of the list by 3 and returning the updated list.
Input stream: [1,2,3]
Output stream: [3,6,9]
flatMap() can be used where we have to flatten or transform out the element, as we cannot flatten our element using map().
Example: Getting the integers of all the Integers present in a List of Integers and returning the result in form of a stream.
Input stream: [[1, 2], 3, [4, 5], []]
Output stream: [1, 2, 3, 4, 5]
The Syntax of the map() is represented as:
<R> Stream<R> map(Function<? super T, ? extends R> mapper)
The Syntax of the flatMap() is represented as:
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
Where R is the element type of the new stream. The stream is an interface and T is the type of stream elements and mapper is a stateless function that is applied to each element and the function returns the new stream.
Difference Between map() and flatmap()
| map() | flatMap() |
|---|---|
| The function passed to map() operation returns a single value for a single input. | The function you pass to flatmap() operation returns an arbitrary number of values as the output. |
| One-to-one mapping occurs in map(). | One-to-many mapping occurs in flatMap(). |
| Only perform the mapping. | Perform mapping as well as flattening. |
| Produce a stream of value. | Produce a stream of stream value. |
| map() is used only for transformation. | flatMap() is used both for transformation and mapping. |
Comments
Post a Comment