WEBVTT

00:00.350 --> 00:00.980
Hi everyone.

00:00.980 --> 00:02.370
Welcome back to this tutorial.

00:02.390 --> 00:08.450
In this tutorial we will code and learn about the map method which is part of the Streams API.

00:08.510 --> 00:14.090
Before we get to the coding part of it, let's talk about it a bit and then get into the coding part.

00:14.300 --> 00:22.970
Basically the map method in streams API is used to change the type of the stream from one type to another.

00:23.000 --> 00:26.170
We have seen some of the examples of the map method before.

00:26.180 --> 00:30.530
Now we are going to dig in further and then explore the map method.

00:30.800 --> 00:32.150
Don't get confused.

00:32.150 --> 00:36.980
This with the map collection that we have as part of the Java APIs.

00:37.070 --> 00:38.720
Let's go ahead and code this one.

00:38.750 --> 00:40.400
Let's go to IntelliJ.

00:42.570 --> 00:43.290
There you go.

00:43.320 --> 00:50.910
What I'm going to do in the streams package, I'm going to create a class called streams Map, Example

00:51.360 --> 00:54.120
Streams map Example.

00:55.150 --> 00:59.350
And then I'm going to make this class executable by adding public static void main.

00:59.710 --> 01:00.520
There you go.

01:01.470 --> 01:01.680
Okay.

01:01.680 --> 01:07.320
The use case that we are going to code here is that we have the student database class, which has all

01:07.320 --> 01:08.270
the students.

01:08.280 --> 01:10.380
The use case is to just.

01:11.660 --> 01:17.490
Elect all these names of the student in a list and then print that list in the console.

01:17.510 --> 01:19.430
That's a use case that we are going to code.

01:19.790 --> 01:25.100
So for this purpose, what I'm going to do, I'm going to create a method called.

01:25.990 --> 01:26.950
Names.

01:30.650 --> 01:35.150
And then I'm going to make this class, make this method static.

01:35.180 --> 01:36.050
What is that?

01:36.050 --> 01:36.890
It is going to return.

01:36.890 --> 01:40.070
It is going to return a list of string.

01:40.070 --> 01:43.910
Basically, it is going to just return the current names.

01:43.910 --> 01:44.570
Right.

01:44.690 --> 01:46.310
Let me put this in presentation mode.

01:47.280 --> 01:49.980
So this is going to return this names list.

01:50.280 --> 01:55.350
The first step is we are going to get the list of students.

01:55.590 --> 01:59.840
You can get that by calling student database dot get all students.

01:59.850 --> 02:03.270
So this is going to give you the list of all the students.

02:03.300 --> 02:06.540
The next step is we are going to create the string.

02:07.260 --> 02:08.070
That's right.

02:08.100 --> 02:09.160
What is the next step?

02:09.180 --> 02:12.270
The next step is we are going to explore the map method.

02:12.270 --> 02:17.520
So we have lot of methods which are part of the stream, but the method that we are going to be using

02:17.520 --> 02:18.930
here is the map method.

02:18.960 --> 02:19.860
The map method.

02:19.860 --> 02:21.960
If you take a look at it, it accepts a function.

02:22.290 --> 02:27.180
So function we are going to implement it by using the method reference.

02:27.480 --> 02:28.980
What is the function in general?

02:28.980 --> 02:33.270
It is going to take in a type and then it is going to return a type, right?

02:33.270 --> 02:40.030
So here the input is going to be the student, but the output that we need is just the names, right?

02:40.050 --> 02:42.030
So how we can implement that.

02:43.400 --> 02:44.150
A student.

02:44.420 --> 02:44.810
Colon.

02:44.810 --> 02:45.320
Colon.

02:45.860 --> 02:46.820
And then.

02:48.250 --> 02:49.480
Let's import the student.

02:50.330 --> 02:51.290
As soon as you.

02:52.900 --> 02:54.520
Press control space here.

02:54.550 --> 02:56.380
You're going to get all these recommendations.

02:56.380 --> 02:58.480
What is that we are going to be focusing on?

02:58.510 --> 03:00.760
We are going to be focusing on the name.

03:00.940 --> 03:05.020
So here what is happening is just going to comment it here.

03:05.020 --> 03:07.870
So you're getting the student as an input.

03:09.530 --> 03:16.460
And then the output is going to be the student name, not the whole student object.

03:16.490 --> 03:21.680
The student object is coming as an input to this map method, we are mapping that student.

03:21.710 --> 03:28.300
We are transforming that student object to a name which is of type string.

03:28.380 --> 03:32.060
So if we go and take a look at the name, the name is of type string.

03:32.210 --> 03:33.080
So.

03:34.790 --> 03:37.190
The output of this one is.

03:38.290 --> 03:39.250
Stream of.

03:40.220 --> 03:40.760
Shouldn't.

03:41.390 --> 03:43.550
The output of this one is.

03:45.810 --> 03:47.820
Stream of string.

03:47.970 --> 03:54.720
So this map method is converting the student object to a string object.

03:55.490 --> 04:01.220
And what we are going to do, we are going to collect this whole stream of string.

04:02.390 --> 04:03.080
Your list.

04:03.110 --> 04:05.780
That's what we are going to do list.

04:07.670 --> 04:13.500
And then if you take a look at it, there is a static import java.util stream dot collectors to list.

04:13.520 --> 04:21.020
So we are going to use the collectors to list method in order to collect all the names as a list.

04:21.670 --> 04:22.360
There you go.

04:22.540 --> 04:28.450
Now, what I'm going to do, I can either add a written statement here is going to work, or what we

04:28.450 --> 04:33.100
can do is we can add a list of string in here.

04:33.100 --> 04:36.490
What we are going to do, we are going to add the.

04:39.380 --> 04:41.870
Name a student list equal to.

04:42.200 --> 04:44.030
And then we will return this.

04:44.900 --> 04:45.800
Either way is fine.

04:45.810 --> 04:50.660
You can either add the return statement directly here or you can assign the result to a list and then

04:50.660 --> 04:51.200
return it.

04:51.350 --> 04:56.060
Now what I'm going to do, I'm going to print this.

04:56.150 --> 04:57.290
I'm going to.

04:58.980 --> 05:00.210
Print the names list.

05:03.080 --> 05:04.010
Good, Right.

05:04.040 --> 05:05.150
Let's run this one.

05:05.860 --> 05:07.590
So what is the result of this one?

05:07.600 --> 05:11.590
It is going to give you the list of string.

05:13.300 --> 05:13.670
Right.

05:13.670 --> 05:14.600
Let's run this one.

05:15.260 --> 05:15.770
We run this.

05:15.770 --> 05:20.910
It is going to just give you the names of the student as a list.

05:20.930 --> 05:26.680
So the map method is basically used to convert from one type to another type.

05:26.690 --> 05:28.310
It is not just that.

05:28.310 --> 05:32.660
I'm going to add some enhancements to this use case, which we just coded.

05:32.670 --> 05:34.400
What I'm going to do is.

05:35.370 --> 05:38.720
I'm going to perform some operation on this one.

05:38.730 --> 05:42.390
So here we are getting this name right, which is of type string.

05:43.220 --> 05:43.990
What I'm going to do.

05:44.000 --> 05:49.070
I'm going to use a string class and then use the method reference syntax.

05:49.220 --> 05:52.330
I'm going to perform some kind of operation on that.

05:52.340 --> 05:54.320
So what is the operation that I can do?

05:54.830 --> 05:57.290
Here I'm performing a uppercase operation.

05:57.290 --> 06:03.590
So the difference between this map and this map is that this map method is converting the whole type.

06:03.620 --> 06:07.360
But if you take a look at this map method, it is not converting the type.

06:07.370 --> 06:12.350
It is performing some kind of operation on the string input.

06:13.420 --> 06:16.870
So the output is again going to be a stream of string, but.

06:17.830 --> 06:23.380
It performs uppercase operation on each input.

06:26.270 --> 06:26.780
Right.

06:26.780 --> 06:33.380
Let's run this and check the output is going to have all the string with uppercase operation.

06:33.410 --> 06:34.040
There we go.

06:34.040 --> 06:42.470
So the result is a collection of names, but it has the uppercase operations performed on top of it.

06:42.680 --> 06:45.230
Now, I do not want the list.

06:45.260 --> 06:48.200
I want the set.

06:48.740 --> 06:49.730
How can we do that?

06:50.000 --> 06:54.500
There is a handy method which is available as part of the collectors here.

06:54.530 --> 07:01.040
What I'm going to do instead of this names list, it's going to be a names set.

07:01.220 --> 07:06.470
And in here, what we are going to do instead of the two list, we are going to use two set.

07:08.110 --> 07:12.040
Okay, so this is going to be set of string.

07:12.490 --> 07:14.380
And then let's import this.

07:15.160 --> 07:15.880
There we go.

07:16.060 --> 07:18.010
So we have to change this one also to set.

07:19.980 --> 07:25.620
So here we have changed the type of the collect method.

07:25.650 --> 07:31.560
So the collect method is going to collect a set of string, not the list of strings.

07:32.340 --> 07:33.240
Going to print that.

07:33.240 --> 07:36.870
Also, it's going to give me the same result, but the collection type is different.

07:36.870 --> 07:43.230
I'm just showing you the different types of collectors that we can use in order to achieve or in order

07:43.230 --> 07:45.900
to get your desired result.

07:47.660 --> 07:48.020
There you go.

07:48.710 --> 07:50.120
So I'm going to run this.

07:51.170 --> 07:54.740
It is again going to give you the same result.

07:54.740 --> 07:58.660
But the thing is, the first one is returning a type of list.

07:58.670 --> 08:01.550
The second one is returning a type of set.

08:02.630 --> 08:06.410
So this is all about map at the moment.

08:06.440 --> 08:11.930
There are a lot of other operations which we can perform, but for now we can stop with this and we're

08:11.930 --> 08:16.100
going to focus on some more methods of the stream operation in the next tutorial.

08:16.130 --> 08:18.230
With this, we came to the end of this tutorial.

08:18.260 --> 08:19.370
Thank you for watching.
