Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and learn about find first and find any function which is part of the
streams API.
This is basically used to find an element in the stream.
The only difference between this one and all match any match and none match is that all.
Match.
None match.
And any match returns a boolean.
This one will return the element itself.
Both the functions returns the result of the type as optional.
So in the previous one for the non-match all match and any match the return type was always boolean.
The Findfirst returns.
The first element it finds as per the condition.
Find any returns.
The first encountered element in the string.
You might be wondering what is the difference, right?
Both the functions are going to return.
The first element it is going to find as per the condition in the stream.
Right.
So the answer for that one is that.
When you run the same stream in the parallel context, the Findfirst is going to return the first element
it encountered in one of the stream which is running in parallel.
So that's where the key difference is going to be.
Don't worry about it.
For now, I'm going to cover parallel streams as a separate section.
I'll cover this during that particular section.
Let's go ahead and code this one.
There you go.
What I'm going to do is for the purpose of this tutorial, I'm going to create a class called Dreams.
Find any first example.
Want to make this class executable.
And.
In presentation mode.
The first function that we are going to explore is find any public.
Tadic.
So we know it is going to return optional.
And the type that we are going to explore is student code that.
For the optional in here.
We are going to do find any student.
That's going to be student database dot.
It all.
Students dot stream.
Okay.
The next step is we are going to use a filter and then use the find any.
Filter.
So we are going to find any student.
Any student who has a GPA as.
Greater than or equal to 3.9.
Okay.
And then I'm gonna.
Ball.
So this is a filter.
The next thing is I'm going to call the find any.
There you go.
There are there is a fine first and find any.
We are going to explore find any as part of this tutorial.
So we have the implementation ready.
Let's call this method and get the result.
The optional.
Shouldn't.
And then.
To make student optional.
Find any?
People to.
Find a new student.
So this is going to give you the optional student if.
Find any.
God is present.
Then I'm going to call the.
Get method and get the result.
Found the student.
Call the get get method Plus.
Didn't know that, Right.
The next thing is.
If the student is not found, then we have to go to the else loop and print it as.
Student not found.
Now, let's go ahead and explore this.
Basically, let's go ahead and run this and check the result.
Run it.
It gave me the student as Emily.
Let's go ahead and check the get all students method.
So you have.
A student, which is going to be 3.6.
So we know Stream is going to pass the elements one by one.
So the first student that it might have passed is 3.6 and the second student is.
Jenny.
So will be in this order, right?
Adam.
Next one is Jenny.
And next one is Emily.
As soon as it found a student with this filter criteria, it returned that student and it did not execute
any of the students below this because it found the student.
That's the purpose of find any.
It just iterates and then it goes through this condition until it finds this find this student basically
until it matches this filter criteria.
And then as soon as one student matches this filter criteria, it returned that student as an output.
It did not execute.
It did not iterate through all the elements in this particular students.
Dream.
All right.
So this is all about find any students.
Let's go ahead and explore the.
Find first student.
So we are going to explore the find first function, which is part of the Streams API.
The place of this we're going to give find first.
Here.
I'll copy this whole thing and then put it here.
Going to be fined.
It's going to be fine.
First student.
Rigor defined for student is present and then you're going to print the result.
If the student is not found, we are going to print.
Student Not found.
I'm going to run this example.
So if you take a look at it, both the output printed the same value.
As I mentioned in the presentation, the difference is going to be when we run the stream in parallel
mode.
Let's not worry about it for now.
I will cover that as a separate section in the future tutorials.
Let's go ahead and explore a scenario where we're going to get a student not found basically if iterates
through all the elements in the stream, but none of the student matches this condition.
I'm going to give 4.1.
I'm I've changed that value to the find first student.
Let's go ahead and run this and then check.
So we are going to get student not found for the find first student method call.
There we go.
So it gave the value as a student not found because none of the student matched this filter criteria.
That's the reason why we did not get the result.
Let me revert it back to its original state.
There you go.
So this is all about find first and find any.
The only difference is that only difference between the all match, any match and none match and find
any and find first is that it returns the actual element itself.
But the all match none match and any match returns the boolean.
If that particular element is present in the stream.
With this, we came to the end of this tutorial.
Thank you for watching.