Skip to content
Related Articles

Related Articles

Improve Article

Stream map() in Java with examples

  • Difficulty Level : Basic
  • Last Updated : 06 Dec, 2018
Geek Week

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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :