WEBVTT

00:00.630 --> 00:01.170
Hi everyone.

00:01.170 --> 00:02.400
Welcome back to this tutorial.

00:02.400 --> 00:08.280
In this tutorial we will code and learn about the flatMap function which is part of the streams API.

00:08.700 --> 00:12.360
Basically the flatmap is similar to the map function.

00:12.360 --> 00:21.060
It is going to transform from one type to the another type, or it can transform from one form of the

00:21.060 --> 00:28.200
same type to another form of the same type, but it is used in the context of stream, where each and

00:28.200 --> 00:31.590
every element in the stream represents multiple elements.

00:31.830 --> 00:38.340
So examples are if you have a stream of list or stream of arrays, basically each and every element

00:38.340 --> 00:39.960
in the stream represents a list.

00:39.960 --> 00:43.080
Each and every element in the stream represent arrays.

00:44.500 --> 00:49.690
Because we all know that lists are arrays can have multiple elements.

00:49.720 --> 00:52.900
I just provided arrays and list as an example.

00:52.900 --> 00:57.160
It can be any type of collection that represents multiple elements.

00:57.190 --> 00:59.170
Let's go ahead and code this one.

01:00.240 --> 01:02.000
I have the IntelliJ open.

01:02.010 --> 01:08.850
What I'm going to do is in the streams package, I'm going to create a new class called Streams flatMap

01:08.850 --> 01:09.630
example.

01:10.840 --> 01:11.420
Streams.

01:12.100 --> 01:12.550
Map.

01:13.470 --> 01:18.510
Example, I'm going to make this class executable by adding public static void main.

01:19.020 --> 01:19.590
There you go.

01:19.620 --> 01:22.020
Let me put this in presentation mode.

01:27.380 --> 01:33.830
So now what I'm going to do, let's go ahead and visit the student database class.

01:35.480 --> 01:36.680
Student database.

01:38.170 --> 01:38.650
In here.

01:38.650 --> 01:46.780
If you take a look at it, the student database has name, grade level GPA, gender and the activities.

01:46.780 --> 01:50.050
These are the activities which represent a list.

01:50.080 --> 01:55.840
What I want is I want to print all the activities in a list.

01:55.840 --> 01:56.950
That is my goal.

01:57.030 --> 02:01.210
So I'm going to use the Streams API in order to perform that activity.

02:04.710 --> 02:07.350
So I'm going to create a method called.

02:08.540 --> 02:10.460
Public static.

02:10.790 --> 02:17.840
So activities are of type string, list of string and then print.

02:19.120 --> 02:20.380
Student activities.

02:24.060 --> 02:24.660
I'm gonna.

02:25.790 --> 02:26.750
For this.

02:28.410 --> 02:29.100
There you go.

02:30.480 --> 02:30.960
Good.

02:31.140 --> 02:36.930
Now what I'm going to do, I'm going to use a student database class, and then I'm going to call the

02:36.960 --> 02:38.170
get all students.

02:38.190 --> 02:44.880
This is going to give you the list of all the students that are available as part of the student database.

02:45.060 --> 02:49.020
I'm going to call the stream method, which is going to create a stream.

02:49.230 --> 02:51.340
So the next step, what is that I'm going to do?

02:51.360 --> 02:54.390
I'm going to do map of.

02:56.460 --> 02:57.240
You don't.

02:58.480 --> 02:59.500
So what is that we want?

02:59.530 --> 03:00.900
We want the activity.

03:00.910 --> 03:02.800
Student get activities.

03:02.830 --> 03:03.550
Right.

03:03.670 --> 03:04.720
Okay, so.

03:05.670 --> 03:08.720
Let's write the code comments here.

03:08.730 --> 03:12.710
Basically, this is going to give you a stream of student.

03:13.730 --> 03:15.050
Right, That's good.

03:15.050 --> 03:21.620
And then we are mapping the student to this list of string meaning, which has all the activities.

03:21.650 --> 03:26.690
This is going to give you a stream of list of.

03:28.210 --> 03:29.020
Think so.

03:29.020 --> 03:32.620
Each and every element in the stream is going to be a list of string.

03:32.710 --> 03:36.850
Now, if I try to do a collect, right, so let's say I'm going to do a collect.

03:39.180 --> 03:40.200
Of tallest.

03:42.090 --> 03:43.740
So this is what we have done before.

03:43.740 --> 03:46.830
If we just want to collect the names, we just do this.

03:48.100 --> 03:48.310
What?

03:48.310 --> 03:49.150
Static method?

03:50.360 --> 03:50.690
Okay.

03:50.690 --> 03:52.310
And then let's do the.

03:53.860 --> 03:54.970
List of string.

03:57.140 --> 03:58.430
Going to be student.

03:59.780 --> 04:00.770
Activities.

04:01.490 --> 04:02.060
Now, if we.

04:02.060 --> 04:03.530
As soon as I did that.

04:04.370 --> 04:06.680
Let's return this one to return.

04:07.820 --> 04:09.080
Student activities.

04:10.350 --> 04:11.820
So here it is, complaining.

04:11.820 --> 04:12.090
Right.

04:12.090 --> 04:15.630
Let's go ahead and take a look at that error.

04:15.660 --> 04:19.710
No instances of type variable exist.

04:19.710 --> 04:23.790
So that list of string conforms to string.

04:23.790 --> 04:29.250
Basically, it is looking for a stream of string, not the stream of list of string.

04:29.250 --> 04:32.070
That's when the flatmap comes into picture.

04:32.070 --> 04:37.800
So here each and every element in the stream is going to reference a list of string.

04:37.800 --> 04:40.560
In those kind of scenarios you need to use flatMap.

04:41.030 --> 04:42.050
So I'm going to use flatMap.

04:42.060 --> 04:43.890
Basically, we have to flatten that stream.

04:43.890 --> 04:48.150
That's the actual purpose that it is going to do behind the scenes.

04:48.150 --> 04:50.430
And then we are going to do list.

04:50.460 --> 04:50.790
Colon.

04:50.790 --> 04:51.480
Colon.

04:53.040 --> 04:53.330
Spring.

04:53.880 --> 04:55.890
So we all know.

04:55.890 --> 04:58.890
So here also I'm using a list we're calling a stream method.

04:58.890 --> 05:01.290
It creates a stream of student here.

05:01.380 --> 05:02.700
This is a list of string.

05:02.700 --> 05:09.840
When you flatten it by calling the flatMap, it is going to create a stream of string here.

05:11.140 --> 05:12.190
Now.

05:13.570 --> 05:18.460
It will be able to collect each and every element in that activities.

05:20.660 --> 05:21.110
List.

05:22.110 --> 05:24.350
Now what we will do, we will print.

05:24.600 --> 05:25.050
Okay.

05:25.240 --> 05:26.610
So I'm going to call.

05:27.960 --> 05:29.490
Gonna have the sea salt.

05:30.550 --> 05:31.870
You typing this out?

05:31.900 --> 05:33.460
It's going to give me the shortcut.

05:33.820 --> 05:37.000
I'm going to give the print student activities.

05:40.330 --> 05:41.830
And I'm going to call this method.

05:41.860 --> 05:46.540
Now, if I run this, basically it is going to give me all the.

05:47.580 --> 05:51.180
List of activities that students are participating.

05:51.910 --> 05:54.310
And let's do a quick recap of what we did.

05:54.490 --> 05:56.680
So flatMap.

05:56.710 --> 05:59.920
We have used Map and flatMap here, actually.

05:59.950 --> 06:06.340
So flatMap is used in scenarios where each and every element in the stream, like in this example,

06:06.340 --> 06:07.540
it represents a list.

06:07.570 --> 06:10.690
Let's say if we have a set, you can still use set colon.

06:10.690 --> 06:11.590
Colon string.

06:11.680 --> 06:17.350
So in those kind of scenarios, you will use flatMap instead of a regular map.

06:18.590 --> 06:21.710
That's the one thing which we have to register in our mind.

06:22.680 --> 06:22.980
With this.

06:22.980 --> 06:25.020
We came to the end of the tutorial.

06:25.140 --> 06:26.220
Thank you for watching.
