Java 8 Streams: difference between map and flatMap

Difference between map() and flatMap():
The map() operates on the individual elements of the stream and returns a value as a Stream with the results. The flatMap() operates on the individual elements of the stream and returns a value as a Stream with the results. In this they are similar.
However the map() takes in a function that returns any R (could be a Stream as well) whereas the flatMap() operation takes in a function that returns a Stream.

Leave a Reply

Your email address will not be published. Required fields are marked *

     

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">