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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Character Stream Vs Byte Stream in Java
- Difference between Stream.of() and Arrays.stream() method in Java
- Program to convert a Map to a Stream in Java
- Flatten a Stream of Map in Java using forEach loop
- Initialize a static Map using Stream in Java
- How to convert a Stream into a Map in Java
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Initialize a static Map using Java 9 Map.of()
- Stream skip() method in Java with examples
- Stream.reduce() in Java with examples
- Stream.max() method in Java with Examples
- Stream min() method in Java with Examples
- Stream generate() method in Java with examples
- Stream count() method in Java with examples
- Stream.of(T... values) in Java with examples
- Stream.of(T t) in Java with examples
- Stream peek() in Java with examples
- Stream mapToInt() in Java with examples
- Stream mapToLong() in Java with examples
- Stream mapToDouble() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.