Stream map() in Java with examples
Stream map(Function mapper) returns a stream consisting of the results of applying the given function to the elements of this stream.
Stream map(Function mapper) is an intermediate operation. These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.
<R> Stream<R> map(Function<? super T, ? extends R> mapper) where, R is the element type of the new stream. Stream is an interface and T is the type of stream elements. mapper is a stateless function which is applied to each element and the function returns the new stream.
Example 1 : Stream map() function with operation of number * 3 on each element of stream.
The stream after applying the function is : 9 18 27 36 45
Example 2 : Stream map() function with operation of converting lowercase to uppercase.
The stream after applying the function is : [GEEKS, GFG, G, E, E, K, S]
Example 3 : Stream map() function with operation of mapping string length in place of string.
The stream after applying the function is : 5 3 9 8 7 3
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.