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.
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
Recommended Posts:
- Stream.of(T t) in Java with examples
- Stream anyMatch() in Java with examples
- Stream allMatch() in Java with examples
- Stream mapToDouble() in Java with examples
- Stream mapToLong() in Java with examples
- Stream flatMapToDouble() in Java with examples
- Stream findFirst() in Java with examples
- Stream flatMapToInt() in Java with examples
- Stream flatMapToLong() in Java with examples
- Stream flatMap() in Java with examples
- Java Stream findAny() with examples
- Stream filter() in Java with examples
- Stream mapToInt() in Java with examples
- Stream min() method in Java with Examples
- Stream.max() method 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 contribute@geeksforgeeks.org. 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.



