WEBVTT

00:01.280 --> 00:01.880
Hi everyone.

00:01.880 --> 00:03.230
Welcome back to this tutorial.

00:03.230 --> 00:08.530
In this tutorial we will code and learn about the filter function which is part of the streams.

00:08.540 --> 00:09.320
API.

00:09.440 --> 00:13.670
Filter function is used to filter the elements in the stream.

00:13.670 --> 00:18.470
The input to the filter is a predicate functional interface.

00:18.500 --> 00:22.910
We have seen a lot of examples about filter in the previous tutorials.

00:22.910 --> 00:28.520
If you are pretty confident about filter function, you can skip this tutorial and move on to the next

00:28.520 --> 00:29.090
one.

00:29.120 --> 00:30.950
Let's go ahead and code this one.

00:33.710 --> 00:35.750
So I'm going to expand the strings package.

00:35.750 --> 00:38.420
I'm going to create a class called.

00:39.360 --> 00:41.550
Dreams filter.

00:42.890 --> 00:43.730
Example.

00:45.140 --> 00:51.200
And the next step is I'm going to make this class executable by adding public static, void main method.

00:52.470 --> 00:54.330
Let me put this in presentation mode.

00:57.370 --> 00:57.910
Here we go.

00:58.390 --> 01:04.300
The next step is I'm going to create a method called public static.

01:05.550 --> 01:06.240
List of.

01:07.530 --> 01:08.340
Students.

01:09.830 --> 01:14.510
And then the method name is going to be filter students.

01:15.570 --> 01:16.560
Simple this.

01:18.750 --> 01:19.770
Let's import this one to.

01:20.730 --> 01:22.500
Java dot util package.

01:22.770 --> 01:30.960
The first step is I'm going to call the student database dot, get all students.

01:31.590 --> 01:35.000
So this is going to give you the access to the list of students.

01:35.010 --> 01:38.160
And then I'm going to use the stream method.

01:39.240 --> 01:43.290
The start leveraging or start using the stream functions.

01:44.980 --> 01:46.090
And part of the Streams API.

01:46.420 --> 01:49.060
The next step is I'm going to use the filter.

01:49.450 --> 01:53.140
So if we take a look at here, filter takes in a predicate.

01:53.170 --> 01:54.880
Let's implement the predicate.

01:56.520 --> 02:02.740
So each and every element of the stream is going to be passed to this filter function one by one.

02:02.760 --> 02:10.350
So what I'm going to do, I'm going to apply the filter as, okay, let's say I want only the female

02:10.350 --> 02:13.170
students as part of the whole student database.

02:14.090 --> 02:15.110
Get all students method.

02:15.110 --> 02:16.970
So in that case, how can I do that?

02:17.060 --> 02:26.240
So we can achieve that by calling student dot get gender dot equals.

02:27.170 --> 02:27.620
Female.

02:28.870 --> 02:30.370
What it is going to do.

02:31.960 --> 02:36.410
It is going to the stream function is going to pass the students one by one.

02:36.430 --> 02:44.830
If the student is of gender female, then only it will be passed to the next step in the pipeline.

02:44.860 --> 02:48.730
The next step is I'm going to keep this pipeline really simple.

02:48.850 --> 02:51.940
I'm going to do a collect collect of.

02:53.760 --> 02:54.110
List.

02:55.850 --> 02:57.020
Because that's what we are going to do.

02:57.050 --> 02:58.520
We are going to return.

02:59.700 --> 03:01.080
List of students.

03:04.100 --> 03:05.820
So it's going to return the list of students.

03:05.860 --> 03:09.560
So what this one is going to give you, this one is going to give you.

03:11.620 --> 03:12.970
Stream of students.

03:15.330 --> 03:15.870
Student.

03:16.260 --> 03:23.640
And again, this one is also give you a dream of student But here this will filter out.

03:28.640 --> 03:29.930
Filters and.

03:30.850 --> 03:32.530
Sends only the.

03:33.720 --> 03:38.250
Students whose gender is female.

03:39.590 --> 03:44.820
So whoever has a gender as male will not be passed to the next step.

03:44.840 --> 03:47.870
Basically, they will be filtered out from the stream.

03:49.080 --> 03:50.190
Let's print this.

03:52.830 --> 03:54.390
When adults also served here.

03:56.390 --> 03:57.710
And then I'm going to print this.

03:59.410 --> 04:02.500
To hear all this as soon as you run.

04:02.500 --> 04:07.840
This example is going to print the list of students in the console.

04:09.610 --> 04:09.970
There you go.

04:09.970 --> 04:12.880
It printed all the students in order for more readability.

04:12.910 --> 04:20.590
What I'm going to do, I'm going to print it by iterating all the values dot for each.

04:21.750 --> 04:24.960
Then we are going to use the method reference syntax.

04:25.050 --> 04:25.380
Colon.

04:25.380 --> 04:26.070
Colon.

04:26.070 --> 04:26.620
Println.

04:27.890 --> 04:28.400
There we go.

04:28.730 --> 04:29.180
Run this.

04:29.180 --> 04:31.070
It's going to print all the students in.

04:32.130 --> 04:32.880
Single line.

04:33.150 --> 04:38.700
So if we take a look at it, all the students who ever got printed are female and then it printed in

04:38.700 --> 04:42.660
the order just passed from the screen.

04:43.640 --> 04:45.200
This is all about filter method.

04:45.200 --> 04:49.130
And one more thing if you want, you can add a number of filter here.

04:49.760 --> 04:51.770
So if you want, you can add a filter.

04:52.220 --> 04:54.650
Let's say you want to filter out the student.

04:57.000 --> 05:01.320
Whose grade level is above 3.9.

05:01.320 --> 05:02.880
So you want to do that.

05:02.880 --> 05:05.820
So student dot get GPA.

05:06.960 --> 05:12.240
Now the grade level sorry, it's a GPA is is greater than or equal to 3.9.

05:13.680 --> 05:21.990
So if you do that basically from this list, 3.5 and 3.8, Ginny and Sophia will be filtered out of

05:21.990 --> 05:22.370
the list.

05:22.380 --> 05:25.830
If I run this, it's just going to print Emily.

05:26.070 --> 05:26.760
There you go.

05:27.060 --> 05:33.510
So you can add any number of filter operations here in the pipeline and then you can get your desired

05:33.510 --> 05:35.440
result that you are looking for.

05:35.460 --> 05:37.710
With this, we came to the end of this tutorial.

05:37.740 --> 05:39.000
Thank you for watching.
