Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about the map method which is part of the Streams API.
Before we get to the coding part of it, let's talk about it a bit and then get into the coding part.
Basically the map method in streams API is used to change the type of the stream from one type to another.
We have seen some of the examples of the map method before.
Now we are going to dig in further and then explore the map method.
Don't get confused.
This with the map collection that we have as part of the Java APIs.
Let's go ahead and code this one.
Let's go to IntelliJ.
There you go.
What I'm going to do in the streams package, I'm going to create a class called streams Map, Example
Streams map Example.
And then I'm going to make this class executable by adding public static void main.
There you go.
Okay.
The use case that we are going to code here is that we have the student database class, which has all
the students.
The use case is to just.
Elect all these names of the student in a list and then print that list in the console.
That's a use case that we are going to code.
So for this purpose, what I'm going to do, I'm going to create a method called.
Names.
And then I'm going to make this class, make this method static.
What is that?
It is going to return.
It is going to return a list of string.
Basically, it is going to just return the current names.
Right.
Let me put this in presentation mode.
So this is going to return this names list.
The first step is we are going to get the list of students.
You can get that by calling student database dot get all students.
So this is going to give you the list of all the students.
The next step is we are going to create the string.
That's right.
What is the next step?
The next step is we are going to explore the map method.
So we have lot of methods which are part of the stream, but the method that we are going to be using
here is the map method.
The map method.
If you take a look at it, it accepts a function.
So function we are going to implement it by using the method reference.
What is the function in general?
It is going to take in a type and then it is going to return a type, right?
So here the input is going to be the student, but the output that we need is just the names, right?
So how we can implement that.
A student.
Colon.
And then.
Let's import the student.
As soon as you.
Press control space here.
You're going to get all these recommendations.
What is that we are going to be focusing on?
We are going to be focusing on the name.
So here what is happening is just going to comment it here.
So you're getting the student as an input.
And then the output is going to be the student name, not the whole student object.
The student object is coming as an input to this map method, we are mapping that student.
We are transforming that student object to a name which is of type string.
So if we go and take a look at the name, the name is of type string.
So.
The output of this one is.
Stream of.
Shouldn't.
The output of this one is.
Stream of string.
So this map method is converting the student object to a string object.
And what we are going to do, we are going to collect this whole stream of string.
Your list.
That's what we are going to do list.
And then if you take a look at it, there is a static import java.util stream dot collectors to list.
So we are going to use the collectors to list method in order to collect all the names as a list.
There you go.
Now, what I'm going to do, I can either add a written statement here is going to work, or what we
can do is we can add a list of string in here.
What we are going to do, we are going to add the.
Name a student list equal to.
And then we will return this.
Either way is fine.
You can either add the return statement directly here or you can assign the result to a list and then
return it.
Now what I'm going to do, I'm going to print this.
I'm going to.
Print the names list.
Good, Right.
Let's run this one.
So what is the result of this one?
It is going to give you the list of string.
Right.
Let's run this one.
We run this.
It is going to just give you the names of the student as a list.
So the map method is basically used to convert from one type to another type.
It is not just that.
I'm going to add some enhancements to this use case, which we just coded.
What I'm going to do is.
I'm going to perform some operation on this one.
So here we are getting this name right, which is of type string.
What I'm going to do.
I'm going to use a string class and then use the method reference syntax.
I'm going to perform some kind of operation on that.
So what is the operation that I can do?
Here I'm performing a uppercase operation.
So the difference between this map and this map is that this map method is converting the whole type.
But if you take a look at this map method, it is not converting the type.
It is performing some kind of operation on the string input.
So the output is again going to be a stream of string, but.
It performs uppercase operation on each input.
Right.
Let's run this and check the output is going to have all the string with uppercase operation.
There we go.
So the result is a collection of names, but it has the uppercase operations performed on top of it.
Now, I do not want the list.
I want the set.
How can we do that?
There is a handy method which is available as part of the collectors here.
What I'm going to do instead of this names list, it's going to be a names set.
And in here, what we are going to do instead of the two list, we are going to use two set.
Okay, so this is going to be set of string.
And then let's import this.
There we go.
So we have to change this one also to set.
So here we have changed the type of the collect method.
So the collect method is going to collect a set of string, not the list of strings.
Going to print that.
Also, it's going to give me the same result, but the collection type is different.
I'm just showing you the different types of collectors that we can use in order to achieve or in order
to get your desired result.
There you go.
So I'm going to run this.
It is again going to give you the same result.
But the thing is, the first one is returning a type of list.
The second one is returning a type of set.
So this is all about map at the moment.
There are a lot of other operations which we can perform, but for now we can stop with this and we're
going to focus on some more methods of the stream operation in the next tutorial.
With this, we came to the end of this tutorial.
Thank you for watching.