Mapping Operations in Stream
In this lesson, we will look at the mapping operations and the different ways to transform a stream.
We'll cover the following
Mapping operations are those operations that transform the elements of a stream and return a new stream with transformed elements.
We can use a variety of methods to transform a stream into another stream object. The two most common methods used are map()
and flatMap()
.
Understanding map()
The map()
method takes a lambda expression as its only argument and uses it to change every individual element in the stream.
Its return value is a new stream object containing the changed elements.
Below is the method definition:
<R> Stream<R> map(Function<? super T, ? extends R> mapper)
Input Parameter -> A function to apply to each element.
Return Type -> Returns a stream consisting of the results of applying the given function to the elements of the stream.
Let’s look at a basic example of map()
. In the below example, we have a list of names. We need to print all the names on the list in the upper case.
Get hands-on with 1400+ tech skills courses.