Spark : Map vs flatMap

Similarity: Both transform collection A to collection B.

Difference:
* map: One element in A is transformed to one element. One -> one.
Size of B = size of A.
* flatMap: One element in A is transformed to 0 or more elements, then
the elements are connected together. One -> many. Size of B >= size of
A.

Shape of the transformation function:
* map: One element in -> one element out.
* flatMap: One element in -> 0 or more elements out (a collection).

> what is a good use case for each?

 

Leave a Reply