Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the mapping Collector.
Mapping collector applies a transformation function first and then collects the data in a collection.
The collection could be of any type.
It could be a list set.
Let's go ahead and explore this using an example.
I've then open here what I'm going to do.
I'm going to create a.
A class called frames Mapping.
Example.
And then I'm going to make this class executable by adding public static void main.
We put this in presentation mode.
What I'm going to do.
I'm going to call the student database class dot, get all students, and then call the stream method.
Followed by that we are going to call the collect method.
In here we are going to call the mapping method.
Right.
Import this.
Have the static import option available.
I'm going to select that option.
If you take a look at it, mapping takes two input.
One is the function, which is a mapper, and another one is a downstream, which represents what is
a type of collection which you want the result to be collected.
So I just want the student names.
Student.
This is a function.
Colon.
Import the student.
Put class Colon.
Colon.
Get name.
Followed by that you're going to do to.
Wait for this.
There you go.
And then this is going to collect the list of students.
Names which are of type string.
Names list.
We go import this?
What are the java.util package?
Let's print this one.
Names list.
Does this one.
Let's go ahead and run this and check the result.
We printed all the names in the list.
Now.
I'm going to do the same kind of operation I'm going to collect.
I'm going to use the name to be mapped, but I want to collect the result in a set.
Pass to set is going to be set of strings.
Let's import this.
This is going to be I'm going to give it a new name called Names Set.
I'll copy this out.
Put it here.
Let's import this to set to.
This is going to be a static import here like the way we did for to list.
And here I'm going to print the names.
Run this and check the result.
Result.
We printed the it collected the names in a set.
If you remember this, in the past we used to do.
Me for this one without.
The mapping method in the past.
If we just want the names, what we do is we call the map method and then student Colon, Colon, get
name, and then we use the collect.
We give it as to list.
So with this we can avoid one additional.
With this mapping method, we can avoid that additional intermediate operation and then we can perform
everything using the mapping method.
So this is all about mapping method.
With this, we came to the end of this tutorial.
Thank you for watching.