Open In App

Stream map() in Java with examples

Improve
Improve
Like Article
Like
Save
Share
Report

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.




// Java code for Stream map(Function mapper)
// to get a stream by applying the
// given function to this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        System.out.println("The stream after applying "
                           + "the function is : ");
  
        // Creating a list of Integers
        List<Integer> list = Arrays.asList(3, 6, 9, 12, 15);
  
        // Using Stream map(Function mapper) and
        // displaying the corresponding new stream
        list.stream().map(number -> number * 3).forEach(System.out::println);
    }
}


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.




// Java code for Stream map(Function mapper)
// to get a stream by applying the
// given function to this stream.
import java.util.*;
import java.util.stream.Collectors;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        System.out.println("The stream after applying "
                           + "the function is : ");
  
        // Creating a list of Integers
        List<String> list = Arrays.asList("geeks", "gfg", "g",
                                          "e", "e", "k", "s");
  
        // Using Stream map(Function mapper) to
        // convert the Strings in stream to
        // UpperCase form
        List<String> answer = list.stream().map(String::toUpperCase).
        collect(Collectors.toList());
  
        // displaying the new stream of UpperCase Strings
        System.out.println(answer);
    }
}


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.




// Java code for Stream map(Function mapper)
// to get a stream by applying the
// given function to this stream.
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        System.out.println("The stream after applying "
                           + "the function is : ");
  
        // Creating a list of Strings
        List<String> list = Arrays.asList("Geeks", "FOR", "GEEKSQUIZ",
                                          "Computer", "Science", "gfg");
  
        // Using Stream map(Function mapper) and
        // displaying the length of each String
        list.stream().map(str -> str.length()).forEach(System.out::println);
    }
}


Output :

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


Last Updated : 06 Dec, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads