WEBVTT

00:00.680 --> 00:01.220
Hi everyone.

00:01.220 --> 00:02.660
Welcome back to this tutorial.

00:02.660 --> 00:08.960
In this tutorial we will code and learn about find first and find any function which is part of the

00:08.960 --> 00:09.890
streams API.

00:11.140 --> 00:15.350
This is basically used to find an element in the stream.

00:15.370 --> 00:21.580
The only difference between this one and all match any match and none match is that all.

00:21.580 --> 00:21.990
Match.

00:22.000 --> 00:22.620
None match.

00:22.630 --> 00:25.000
And any match returns a boolean.

00:25.030 --> 00:28.540
This one will return the element itself.

00:30.170 --> 00:34.580
Both the functions returns the result of the type as optional.

00:34.610 --> 00:41.150
So in the previous one for the non-match all match and any match the return type was always boolean.

00:42.000 --> 00:43.500
The Findfirst returns.

00:43.500 --> 00:46.620
The first element it finds as per the condition.

00:48.400 --> 00:49.210
Find any returns.

00:49.210 --> 00:52.630
The first encountered element in the string.

00:53.610 --> 00:55.900
You might be wondering what is the difference, right?

00:55.920 --> 00:58.320
Both the functions are going to return.

00:58.320 --> 01:03.060
The first element it is going to find as per the condition in the stream.

01:03.060 --> 01:03.600
Right.

01:03.600 --> 01:05.970
So the answer for that one is that.

01:06.800 --> 01:13.760
When you run the same stream in the parallel context, the Findfirst is going to return the first element

01:13.760 --> 01:18.240
it encountered in one of the stream which is running in parallel.

01:18.260 --> 01:21.020
So that's where the key difference is going to be.

01:21.050 --> 01:22.010
Don't worry about it.

01:22.010 --> 01:26.820
For now, I'm going to cover parallel streams as a separate section.

01:26.840 --> 01:30.020
I'll cover this during that particular section.

01:31.620 --> 01:33.090
Let's go ahead and code this one.

01:37.000 --> 01:37.460
There you go.

01:37.480 --> 01:43.760
What I'm going to do is for the purpose of this tutorial, I'm going to create a class called Dreams.

01:44.200 --> 01:47.350
Find any first example.

01:50.000 --> 01:51.830
Want to make this class executable.

01:53.810 --> 01:54.440
And.

01:56.110 --> 01:57.310
In presentation mode.

02:00.020 --> 02:05.780
The first function that we are going to explore is find any public.

02:06.430 --> 02:06.910
Tadic.

02:08.920 --> 02:11.710
So we know it is going to return optional.

02:12.040 --> 02:17.530
And the type that we are going to explore is student code that.

02:18.610 --> 02:21.100
For the optional in here.

02:21.100 --> 02:24.600
We are going to do find any student.

02:27.490 --> 02:30.190
That's going to be student database dot.

02:31.270 --> 02:31.780
It all.

02:31.780 --> 02:33.510
Students dot stream.

02:33.790 --> 02:34.380
Okay.

02:34.390 --> 02:39.090
The next step is we are going to use a filter and then use the find any.

02:40.220 --> 02:40.880
Filter.

02:41.480 --> 02:44.030
So we are going to find any student.

02:45.120 --> 02:48.240
Any student who has a GPA as.

02:49.270 --> 02:52.210
Greater than or equal to 3.9.

02:53.580 --> 02:53.940
Okay.

02:54.060 --> 02:55.350
And then I'm gonna.

02:56.530 --> 02:57.010
Ball.

02:57.010 --> 02:58.390
So this is a filter.

02:58.610 --> 03:02.050
The next thing is I'm going to call the find any.

03:03.130 --> 03:03.480
There you go.

03:03.490 --> 03:05.410
There are there is a fine first and find any.

03:05.410 --> 03:09.700
We are going to explore find any as part of this tutorial.

03:13.760 --> 03:15.290
So we have the implementation ready.

03:15.290 --> 03:17.570
Let's call this method and get the result.

03:18.320 --> 03:19.370
The optional.

03:20.940 --> 03:21.780
Shouldn't.

03:25.140 --> 03:25.900
And then.

03:26.970 --> 03:28.700
To make student optional.

03:28.970 --> 03:29.900
Find any?

03:33.080 --> 03:33.620
People to.

03:34.890 --> 03:36.240
Find a new student.

03:36.550 --> 03:40.230
So this is going to give you the optional student if.

03:41.300 --> 03:42.230
Find any.

03:45.020 --> 03:46.550
God is present.

03:46.850 --> 03:48.530
Then I'm going to call the.

03:50.690 --> 03:52.600
Get method and get the result.

03:52.610 --> 03:54.020
Found the student.

03:55.700 --> 03:58.010
Call the get get method Plus.

03:58.980 --> 04:03.460
Didn't know that, Right.

04:04.100 --> 04:05.450
The next thing is.

04:07.720 --> 04:13.570
If the student is not found, then we have to go to the else loop and print it as.

04:14.810 --> 04:17.300
Student not found.

04:19.450 --> 04:20.950
Now, let's go ahead and explore this.

04:20.950 --> 04:23.790
Basically, let's go ahead and run this and check the result.

04:26.230 --> 04:26.770
Run it.

04:27.100 --> 04:29.200
It gave me the student as Emily.

04:29.290 --> 04:32.590
Let's go ahead and check the get all students method.

04:32.590 --> 04:33.910
So you have.

04:35.130 --> 04:37.530
A student, which is going to be 3.6.

04:37.530 --> 04:41.430
So we know Stream is going to pass the elements one by one.

04:41.760 --> 04:46.380
So the first student that it might have passed is 3.6 and the second student is.

04:48.020 --> 04:48.700
Jenny.

04:48.710 --> 04:50.960
So will be in this order, right?

04:51.830 --> 04:52.580
Adam.

04:53.760 --> 04:54.810
Next one is Jenny.

04:55.530 --> 04:57.030
And next one is Emily.

04:58.450 --> 05:06.400
As soon as it found a student with this filter criteria, it returned that student and it did not execute

05:06.400 --> 05:10.480
any of the students below this because it found the student.

05:10.720 --> 05:13.030
That's the purpose of find any.

05:13.060 --> 05:20.620
It just iterates and then it goes through this condition until it finds this find this student basically

05:20.620 --> 05:22.750
until it matches this filter criteria.

05:22.750 --> 05:29.200
And then as soon as one student matches this filter criteria, it returned that student as an output.

05:31.470 --> 05:33.120
It did not execute.

05:33.120 --> 05:38.490
It did not iterate through all the elements in this particular students.

05:39.400 --> 05:39.760
Dream.

05:41.210 --> 05:41.660
All right.

05:41.660 --> 05:44.530
So this is all about find any students.

05:44.540 --> 05:46.280
Let's go ahead and explore the.

05:47.340 --> 05:49.620
Find first student.

05:49.650 --> 05:54.810
So we are going to explore the find first function, which is part of the Streams API.

05:54.870 --> 05:58.080
The place of this we're going to give find first.

06:01.200 --> 06:01.620
Here.

06:02.610 --> 06:05.130
I'll copy this whole thing and then put it here.

06:07.060 --> 06:08.620
Going to be fined.

06:12.000 --> 06:12.930
It's going to be fine.

06:12.960 --> 06:14.130
First student.

06:18.990 --> 06:25.590
Rigor defined for student is present and then you're going to print the result.

06:25.590 --> 06:28.050
If the student is not found, we are going to print.

06:28.080 --> 06:28.950
Student Not found.

06:29.960 --> 06:31.610
I'm going to run this example.

06:35.670 --> 06:39.870
So if you take a look at it, both the output printed the same value.

06:40.760 --> 06:46.550
As I mentioned in the presentation, the difference is going to be when we run the stream in parallel

06:46.550 --> 06:47.000
mode.

06:47.150 --> 06:48.860
Let's not worry about it for now.

06:48.860 --> 06:53.780
I will cover that as a separate section in the future tutorials.

06:55.180 --> 07:01.270
Let's go ahead and explore a scenario where we're going to get a student not found basically if iterates

07:01.270 --> 07:05.590
through all the elements in the stream, but none of the student matches this condition.

07:05.600 --> 07:07.270
I'm going to give 4.1.

07:07.540 --> 07:11.110
I'm I've changed that value to the find first student.

07:11.140 --> 07:13.030
Let's go ahead and run this and then check.

07:13.030 --> 07:18.940
So we are going to get student not found for the find first student method call.

07:21.310 --> 07:21.940
There we go.

07:22.220 --> 07:28.340
So it gave the value as a student not found because none of the student matched this filter criteria.

07:28.370 --> 07:30.920
That's the reason why we did not get the result.

07:31.040 --> 07:33.800
Let me revert it back to its original state.

07:35.090 --> 07:35.800
There you go.

07:35.840 --> 07:39.950
So this is all about find first and find any.

07:39.980 --> 07:45.680
The only difference is that only difference between the all match, any match and none match and find

07:45.680 --> 07:49.880
any and find first is that it returns the actual element itself.

07:49.880 --> 07:54.590
But the all match none match and any match returns the boolean.

07:54.590 --> 07:59.030
If that particular element is present in the stream.

07:59.120 --> 08:01.340
With this, we came to the end of this tutorial.

08:01.340 --> 08:02.570
Thank you for watching.
