WEBVTT

00:00.170 --> 00:00.770
Hi everyone.

00:00.770 --> 00:02.090
Welcome back to this tutorial.

00:02.090 --> 00:08.510
In this tutorial we will code and explore the map method, flat map method and the filter method that

00:08.510 --> 00:10.820
are part of the optional class.

00:10.850 --> 00:19.730
The first step is I'm going to create a class called Optional map Flat Map example.

00:20.000 --> 00:24.020
Let's make this class executable by adding public static void main method.

00:24.350 --> 00:29.540
So the methods that we are going to explore this presentation mode.

00:31.220 --> 00:38.090
Are going to be filter flat, map and map.

00:38.240 --> 00:42.230
So the first method that we are going to take a look at is a filter method.

00:42.560 --> 00:42.950
Right.

00:42.950 --> 00:49.610
I'm going to create a method specific for this one, and this method is not going to return anything

00:49.790 --> 00:54.950
and then the method name is going to be optional filter.

00:55.910 --> 00:58.700
So I'm going to use the student database.

01:00.380 --> 01:07.460
Dart Pyrone supplier Dart get this is going to give me the actual student.

01:07.760 --> 01:12.920
And then we are going to encapsulate that inside the optional object.

01:12.920 --> 01:18.830
So this will return optional of type student.

01:18.860 --> 01:19.610
Right.

01:19.610 --> 01:24.590
The next step is let's get this value optional of student.

01:25.100 --> 01:27.160
Let's give it a name, right?

01:27.170 --> 01:30.350
The name is going to be let's import the student.

01:32.210 --> 01:34.430
The name is going to be student optional.

01:34.460 --> 01:35.040
There you go.

01:35.060 --> 01:41.630
So we have the student optional, which has a student inside it, and the student is going to be Adam.

01:43.080 --> 01:45.600
Now you want to apply some filter, right?

01:45.600 --> 01:51.090
So filter is some operation which we have seen in the previous tutorials as part of the stream operation.

01:51.090 --> 01:55.050
Now optional also have a filter method that it supports.

01:55.050 --> 01:58.410
If you want to apply some filter before you get to the actual value.

01:58.410 --> 02:06.090
So what I'm going to do, I'm going to do student optional dot filter so you can pass a predicate.

02:06.120 --> 02:08.310
The predicate that I'm going to use.

02:08.340 --> 02:16.200
We all know this optional object encapsulate a student, so I'm going to use a student student dot get

02:16.210 --> 02:20.250
GPA greater than or equal to 3.5.

02:20.250 --> 02:23.100
So I'm going to give the value as 3.5.

02:23.130 --> 02:25.710
If that is there, then.

02:26.900 --> 02:28.740
I'm going to build a pipeline here.

02:28.770 --> 02:31.050
If that is there, then do a.

02:32.610 --> 02:38.070
If present, then print the student.

02:40.620 --> 02:42.580
Now let's go ahead and run this one.

02:42.600 --> 02:48.220
So in the public static void main method, I'm going to call the optional filter method.

02:48.240 --> 02:49.860
This is going to.

02:51.540 --> 02:53.050
Check the student GPA.

02:53.050 --> 02:59.080
If it is greater than 3.5, then it is going to get access to that student object and then printing

02:59.080 --> 02:59.230
it.

02:59.230 --> 03:03.220
If you want some other operation that needs to be performed, you are welcome to do it.

03:03.220 --> 03:05.920
But in here I want to keep this example really simple.

03:05.920 --> 03:08.020
That's the reason why I'm just printing it here.

03:08.170 --> 03:10.420
Let's go ahead and check what is the student's GPA?

03:10.450 --> 03:10.900
GPA?

03:10.930 --> 03:12.730
The GPA is 3.6, right?

03:12.760 --> 03:16.420
Let's say I'm going to give the value as 4.2.

03:17.110 --> 03:23.800
So in that case, nothing will happen because this filter is going to filter out the student and the

03:23.800 --> 03:27.520
student won't even reach the if present method.

03:28.000 --> 03:28.570
Right?

03:28.570 --> 03:31.060
So this is all about the filter method.

03:31.060 --> 03:38.410
The filter method gives you the flexibility to apply the filter operations on an optional object.

03:38.410 --> 03:41.260
Again, this is not a stream, this is an optional object.

03:41.290 --> 03:44.980
Now let's go ahead and explore the map method.

03:44.980 --> 03:50.750
I'm going to come to the flatMap method in a bit, but we'll explore the map method.

03:50.750 --> 03:55.130
We talked about map method and we covered map method a bit in the previous tutorial.

03:55.130 --> 03:58.490
I'm just going to give you the similar kind of example.

03:58.490 --> 03:59.690
Nothing new here.

03:59.690 --> 04:07.580
So what I'm going to do, I'm going to do, I'm going to create a method public static void, and then

04:07.580 --> 04:10.010
this is going to be optional map.

04:11.580 --> 04:14.580
And I'm going to do the same operation here.

04:15.510 --> 04:22.770
So now we have access to the optional method in here, what we are going to do.

04:24.620 --> 04:26.900
Now we have the optional student ready.

04:26.930 --> 04:30.350
The next step is we are going to check if the student is present.

04:31.670 --> 04:32.240
Right.

04:32.240 --> 04:33.530
That should be the first check.

04:33.560 --> 04:38.720
Then what we are going to do, we are going to get the name of the student or you can apply.

04:38.720 --> 04:41.480
We can change this one with the filter also.

04:41.630 --> 04:42.680
Let's go ahead and do that.

04:42.710 --> 04:50.930
So this is going to be optional dot filter In this filter, I'm going to use the same code, whatever

04:50.930 --> 04:51.920
that is here.

04:52.310 --> 04:53.000
Give the logic.

04:53.000 --> 04:59.750
This is going to be 3.5 because Adam is having really poor GPA, not poor.

04:59.750 --> 05:00.680
It's an average GPA.

05:00.980 --> 05:02.720
And then what is the next thing?

05:02.720 --> 05:05.660
The next thing is going to be the map method.

05:05.840 --> 05:11.240
In the map method, we are going to get the student name right.

05:11.240 --> 05:12.800
And what is this going to return?

05:12.800 --> 05:17.470
If you go to the map method, Map method returns an object of type optional.

05:17.480 --> 05:18.280
Right?

05:18.290 --> 05:25.020
Let's go ahead and get that optional of type string, because if you take a look at the student name,

05:25.020 --> 05:27.210
this is of type string.

05:27.960 --> 05:35.940
Now what we can do, we can print this, we print it, it is going to give you the actual value that

05:35.940 --> 05:38.340
it encapsulates.

05:38.340 --> 05:42.180
If you run this example, I didn't call this method it.

05:42.570 --> 05:47.070
So in here, what we are going to do, we are going to call the optional map

05:49.500 --> 05:50.400
optional map.

05:51.660 --> 05:53.730
And then let's go ahead and run this example.

05:53.730 --> 05:58.950
So when you run this example, this is going to call this one and it is going to print the name of the

05:58.950 --> 06:00.960
student, which is nothing but Adam.

06:01.930 --> 06:02.330
There you go.

06:02.350 --> 06:04.030
It printed the value as Adam.

06:04.210 --> 06:06.700
Now let's go ahead and explore the flat method.

06:06.700 --> 06:10.660
I'm thinking of covering this flat method as a next tutorial.

06:10.810 --> 06:16.600
For now, we have covered the filter and map method As part of this tutorial, let's go ahead and explore

06:16.600 --> 06:19.600
the flat flat map in the next tutorial.

06:19.600 --> 06:21.660
With this we came to the end of this tutorial.

06:21.670 --> 06:22.840
Thank you for watching.
