The map() and flatmap() are two important operations in new functional Java 8. Both represents functional operation and they are also methods in java.util.stream.Stream class. The key difference between map() and flatmap() function is that when you use map(), it applies a function on each element of stream and stores the value returned by the function into a new Stream. This way one stream is transformed into another e.g. a Stream of String is transformed into a Stream of Integer where each element is length of corresponding Stream. Key thing to remember is that the function used for transformation in map() returns a single value. If map() uses a function, which, instead of returning a single value returns a Stream of values than you have a Stream of Stream of values, and flatmap() is used to flat that into a Stream of values.
Read more »
No comments:
Post a Comment