In this section we will learn what the Stream map() method is and how to use it in Java.
What is Java Stream map() Method?
The Java Stream map() method is used to map the elements of a stream and produce new elements as a result of this mapping.
Basically, this map() method takes a reference to another method and passes each element of the stream as its argument.
In the reference method is the place where we can modify the elements of the stream and return a new one as a result or even just return each element itself.
Note: the method is an intermediate and lazy one.
Java map() Method Syntax:
<R> Stream<R> map(Function<? super T,? extends R> mapper)
map() Method Parameters:
The method has one parameter and that is of type Function, which is a functional interface.
Basically, this method needs a reference to another method.
Here’s the signature of the reference method:
- The method has one parameter and that is of the same type as the elements in the stream.
- The return value of the reference method could be of the same or any other datatype.
Note: we can create this reference method using the lambda expression or refer to an instance or static method that is already created and has the same signature.
map() Method Return Value:
The return value of this method is a new stream.
map() Method Exceptions:
The method might throw two exceptions and that is:
NullPointerException: We get this exception if the argument of this method is set to null.
ClassCastException: We get this exception if the data type of the reference method is of incompatible type compared to the data type of elements in the stream.
Example: using Stream map() method
import java.util.stream.Stream; class Main{ public static void main(String[] args) { Stream<Integer> stream = Stream.of(20,2,3,4,5,6,7,8,9,10,3,23,4,23,423,2,34,56,2,7,86,54,5); stream.peek(e->System.out.println("Is the value " +e+" bigger than the value 50? ")).map(e->e>50?true:false).forEach(e->System.out.println(e)); } }
Output:
Is the value 20 bigger than the value 50? false Is the value 2 bigger than the value 50? false Is the value 3 bigger than the value 50? false Is the value 4 bigger than the value 50? false Is the value 5 bigger than the value 50? false Is the value 6 bigger than the value 50? false Is the value 7 bigger than the value 50? false Is the value 8 bigger than the value 50? false Is the value 9 bigger than the value 50? false Is the value 10 bigger than the value 50? false Is the value 3 bigger than the value 50? false Is the value 23 bigger than the value 50? false Is the value 4 bigger than the value 50? false Is the value 23 bigger than the value 50? false Is the value 423 bigger than the value 50? true Is the value 2 bigger than the value 50? false Is the value 34 bigger than the value 50? false Is the value 56 bigger than the value 50? true Is the value 2 bigger than the value 50? false Is the value 7 bigger than the value 50? false Is the value 86 bigger than the value 50? true Is the value 54 bigger than the value 50? true Is the value 5 bigger than the value 50? false