WEBVTT

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

00:00.890 --> 00:02.180
Welcome back to this tutorial.

00:02.180 --> 00:08.660
In this tutorial we will code and explore the flatMap method that is part of the optional class.

00:09.080 --> 00:14.030
The first step which I'm going to do is that I'm going to introduce a new class called Bike.

00:16.240 --> 00:17.650
New class called Bike.

00:17.830 --> 00:19.900
And this is going to have.

00:20.470 --> 00:30.190
Private string name and model name and model.

00:30.940 --> 00:33.160
And we are going to generate.

00:34.190 --> 00:36.050
The getters and setters for this one.

00:37.020 --> 00:37.560
In red.

00:40.340 --> 00:42.740
Select all the properties and click okay.

00:43.070 --> 00:45.890
And then I'm going to generate the two string method to.

00:49.470 --> 00:50.010
There you go.

00:50.250 --> 00:51.990
So we have the bike object.

00:51.990 --> 00:53.610
Sorry, the bike class created.

00:53.640 --> 00:57.960
Now I'm going to build a relationship between the bike class and the student class.

00:58.800 --> 01:02.870
If you take a look at the student class, we are going to incorporate bike in here.

01:02.880 --> 01:06.870
So the use cases, not all the students will have the bike.

01:06.870 --> 01:10.910
So the students, whoever has a bike, will use their bike to come to the school.

01:10.920 --> 01:12.240
How can we represent that?

01:12.240 --> 01:19.790
So this is going to be an optional property because not all the students is going to have the bike.

01:19.800 --> 01:30.510
How can we represent that using optional so private optional of bike and just give it a name.

01:30.510 --> 01:34.230
This is going to be by default optional dot empty.

01:34.260 --> 01:40.170
So as a developer, just by looking at this model, which is the student class, I can say, okay,

01:40.170 --> 01:46.710
I have an optional object which represent this is not a mandatory property that each and every student

01:46.710 --> 01:47.520
will have.

01:47.790 --> 01:48.630
Right now.

01:48.630 --> 01:51.660
Let's go ahead and generate the getter and setter method for this.

01:53.410 --> 01:55.810
Rate getter and setter.

01:57.040 --> 01:57.670
Okay.

01:57.670 --> 02:01.900
And then let's read the two string also.

02:04.530 --> 02:07.080
Replace the existing two string method, Click on.

02:07.080 --> 02:08.640
Yes, there you go.

02:08.640 --> 02:15.750
And what I'm going to do, as you mentioned, not all the students are going to have this rate at the

02:15.750 --> 02:16.320
bike.

02:16.350 --> 02:21.000
I'm just going to add it to student one or what I can do.

02:21.780 --> 02:35.310
I can have this added just to the supplier bike, bike equal to new bike, and then the bike is going

02:35.310 --> 02:38.430
to have a name and model.

02:38.430 --> 02:44.490
So set name equal to ABC and model equal to.

02:46.990 --> 02:48.940
Let model equal to x, y, z.

02:53.800 --> 02:58.750
So what I'm going to do, I'm going to create a student instance and then return it.

02:59.380 --> 03:09.760
Student Equal to and then Student Dot set bike equal to going to be optional.

03:10.270 --> 03:11.770
Dot off.

03:13.100 --> 03:14.030
Nullable.

03:15.250 --> 03:18.460
Bike and then return this shirt.

03:19.660 --> 03:24.460
So now we added the supplier method, right?

03:24.460 --> 03:29.170
So we added the supplier method in the supplier student supplier.

03:29.170 --> 03:35.530
We added this bike optional to the student object and we are returning that as part of this supplier.

03:35.860 --> 03:37.900
Now let's go ahead and code this.

03:38.700 --> 03:40.890
Using the optional bike.

03:42.340 --> 03:44.030
So what is the scenario that we are going to code?

03:44.050 --> 03:47.080
We are going to code the flat map example.

03:47.950 --> 03:49.720
Let me put this in presentation mode.

03:52.740 --> 04:00.120
In here, what I'm going to do, I'm going to create public static.

04:01.890 --> 04:02.850
Going to be.

04:04.510 --> 04:05.400
flatMap.

04:05.410 --> 04:05.980
Right.

04:06.460 --> 04:07.180
Optional.

04:09.290 --> 04:10.570
flatMap.

04:13.490 --> 04:16.940
And then I'm going to call similar code.

04:21.660 --> 04:25.710
So this is going to give me the it's going to be a wide method.

04:27.510 --> 04:31.680
This is going to give me the optional object back.

04:31.920 --> 04:32.430
Right.

04:32.430 --> 04:37.320
So now my use case is I want to get to the bike name.

04:37.320 --> 04:41.660
I want to get to the bike name and print that bike name as an output.

04:41.670 --> 04:42.840
That's what I want to do.

04:42.870 --> 04:43.860
How do I get to it?

04:44.010 --> 04:52.140
The first thing I'm going to call student optional and then I'm going to have the filter method to filter

04:52.260 --> 04:54.960
the student, the student GPA.

04:57.000 --> 05:01.880
Is greater than 3.5 then only greater than or equal to 3.5.

05:01.910 --> 05:05.180
Then only I want to get to the bike name.

05:05.360 --> 05:06.440
And then.

05:06.740 --> 05:09.860
So what is the type of bike name in the student class?

05:09.860 --> 05:14.420
The type is of type optional, right?

05:14.420 --> 05:19.340
So in order to get to the optional object inside an optional object, because it's already an optional

05:19.340 --> 05:23.870
object, you want to get to the optional object which is inside another optional object.

05:23.870 --> 05:27.320
The only way to get into it is by using flatMap.

05:27.800 --> 05:31.700
So if you remember for the streams API we have used the flatmap.

05:31.700 --> 05:37.400
flatMap is used to flatten the stream which represents multiple elements actually.

05:37.400 --> 05:42.530
So let's say you have a list of elements inside a object.

05:42.530 --> 05:47.480
Then in those kind of scenarios, if you want to flatten that stream to each and every element to represent

05:47.480 --> 05:50.930
a stream, then we have used flatMap in that example.

05:50.930 --> 05:54.560
Now we are going to use flatMap in the optional context.

05:54.560 --> 06:03.180
So in the optional context, flatMap is used to get to the actual value of the optional object inside

06:03.180 --> 06:04.650
another optional object.

06:05.700 --> 06:07.020
Dot get bike.

06:08.510 --> 06:11.390
And we're going to map that to.

06:13.200 --> 06:14.700
Pike dot get name.

06:14.700 --> 06:16.140
This is what we want, right?

06:16.140 --> 06:19.890
And then this is going to return a student.

06:23.220 --> 06:29.550
It is going to return us string name and it's going going to be of type optional.

06:31.600 --> 06:38.200
Was a map again and optional is going to return another optional right here.

06:38.200 --> 06:43.180
It encapsulates the name as a the bike name inside the optional.

06:46.470 --> 06:47.120
Object.

06:47.130 --> 06:53.100
So here we are going to call this method and this is going to print the bike name.

06:53.100 --> 06:55.110
Let's go ahead and execute this program.

06:57.350 --> 07:00.350
If we executed it gave you the optional ABC.

07:00.770 --> 07:02.510
You want to get to the name of it?

07:03.350 --> 07:04.850
We'll do the If percent.

07:08.560 --> 07:11.860
Name Dot if present then.

07:13.450 --> 07:16.630
We are going to print it right now.

07:21.790 --> 07:25.680
Yes, you can give a different value here.

07:25.690 --> 07:26.200
Name is.

07:26.200 --> 07:26.800
Yes.

07:27.520 --> 07:28.630
Let's run this.

07:29.830 --> 07:30.020
Run.

07:30.080 --> 07:31.820
Thus it gave you the name as ABC.

07:32.690 --> 07:34.520
Okay, let's go through this pipeline again.

07:34.520 --> 07:35.840
So what happens in this line?

07:35.840 --> 07:40.580
In this line, this is going to still return optional of shouldn't.

07:40.850 --> 07:45.920
Now we have to access the bike which is inside.

07:45.920 --> 07:49.970
If we take a look at it, the bike will be inside this as an another optional object.

07:49.970 --> 07:50.450
Right.

07:50.480 --> 07:56.660
If you want to get to the access, you want to want to get to the actual bike, you need to perform

07:56.660 --> 07:57.370
the flatMap.

07:57.380 --> 08:02.540
This is going to give you the optional of bike as a output.

08:02.540 --> 08:07.130
That's what it is going to do, but it needs to get into the student object first, which is of type

08:07.130 --> 08:10.970
optional and then get to the bike optional object.

08:10.970 --> 08:13.910
So that's the reason why we need flatMap here.

08:13.910 --> 08:20.870
And this gives optional bike as an output and using that we are mapping and getting the name of that

08:20.870 --> 08:23.570
and returning that as an optional of string.

08:23.570 --> 08:25.820
So this is what happens behind the strings.

08:25.820 --> 08:32.300
So here we have a two levels of optionals that's the reason why we need to have the flatMap.

08:32.310 --> 08:37.950
So here this returns the optional of bike as an output at this step.

08:39.340 --> 08:40.900
I hope the concepts are clear.

08:40.900 --> 08:43.010
So this is all about flatMap.

08:43.030 --> 08:45.190
With this, we came to the end of this tutorial.

08:45.220 --> 08:46.390
Thank you for watching.
