VOOZH about

URL: https://www.geeksforgeeks.org/java/stream-map-java-examples/

⇱ Stream map() in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stream map() in Java with examples

Last Updated : 4 Jan, 2025

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.

Syntax :

<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.

Output :

The stream after applying the function is : 
9
18
27
36
45

Example 2 :

Stream map() function with operation of converting lowercase to uppercase.

Output :

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.

Output :

The stream after applying the function is : 
5
3
9
8
7
3
Comment