Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn the map two Object map to long and map to double function as
part of the streams API.
The first one is map to object.
This is used to convert each element in the numeric stream to some object.
Don't worry, if this doesn't make sense, we'll be able to understand when we actually code this in
a moment.
The next one is map to long.
This is used to convert a numeric stream to a long stream.
Let's say you have a in stream.
If you want to convert that to a long stream, you just have to call this method that is going to take
care of converting that in stream to a long stream.
The next one is map to double.
This is used to convert a numeric stream to a double stream.
We discussed this example already, but I would like to cover this again as part of this tutorial.
Let's go ahead and code this one.
I have the previous examples still here.
I'm going to close this and create a brand new class for this tutorial.
Let me close this one.
This console.
Now we will create a class called Numeric
Stream Map example.
Let's make this class executable by adding public static void main method.
The first method that we are going to explore is map to object.
I'm going to create public static void map to object is a method that we are going to explore.
The first step is I'm going to create a stream dot range and empty range closed.
I'm going to give the value as five.
So it's going to have it's going to create an stream of five elements.
I want to use the map to object method, as you see here, the map to object method.
It takes in an INT function as an input.
So our goal is going to be we are going to take this value.
And then we are going to return a new integer.
Return.
New integer.
And then I'm going to pass the value here.
So in the place of New Integer, if we have any custom object, we can still use it.
But the input for that custom object is going to be this integer.
And after that, what I'm going to do, I'm going to do a collect of.
List.
And I'm going to do a static import of collector, as you see here, the static import of collector.
I'm going to return this return.
What is that?
It is returning.
It is returning a list of integer.
That's what it is going to return.
There you go.
Sounds good.
Let's call this method and print the result.
So I'm going to name this one as map to object.
And then let's call this let's run this example.
I'm going to run this example.
Then as I run the example.
There you go.
So we have successfully used the map to object to convert a int stream to a list of integers.
So in the place of integer, if you have any custom class that just takes in an int as an input and
then creates a object, you can go ahead and use this one.
And the next one is we are going to explore the map too long.
Public static want to create the method with void will change the return type.
After we code our use case completely, it's going to be map to long.
So again, I'm going to use a in stream.
There we go.
And here we're going to use the map to long method in the map to long method.
Basically, you're going to accept an input and return the same input.
And it is going to perform.
I'm going to do a sum sum of all the numbers.
So this one will convert.
In stream to long stream.
If you go to this map to long method, you can see the return type of that method is long stream.
The input that it accepts is the I value that is being passed from here.
This is a in stream and I value is passed from the end string.
There we go.
And what we are going to do, we are going to return this.
Going to be long.
Then run this and check.
Going to be mapped along.
Mapped along.
Go ahead and run this one.
So there you go.
The result is map to long.
The next method, which we are going to explore, it's going to be map to double.
That's the next method.
It's going to be map to double.
So in the place of this, we are going to use map to double.
Okay.
And then the return type is going to be double.
This is going to convert a in stream to.
Double stream and then we are performing summation on that double value.
Okay, I'm going to call this one.
It's going to be map to double.
And this is going to be map to double to this example.
The silt is 15.0.
This of type double.
So this is all about map to object map to long and map to double.
With this we came to the end of this tutorial.
Thank you for watching.