Hi everyone.
Welcome back to this tutorial.
In this tutorial, we'll explore the function interface using that student collection that we have been
using throughout the tutorial.
Now the use cases, we are going to create a map of students and their respective grades meaning and
their respective GPA.
For that purpose.
What I'm going to do, I'm going to create a new class called function student example.
Then I'm going to make this class executable.
SVM.
There you go.
Class is executable now.
The first step is we are going to create the function interface.
Static.
Function interface.
So the input is going to be.
A list of students and the output is going to be a hash map with the student and their GPA.
The list of.
Student is the input.
Followed by that I'm using the option and return to auto import these classes in Mac.
You take a look at the windows, it's alt enter.
And the next thing is it's going to be a map of string.
And double.
The double is a type which is being used to store the student GPAs.
We take a look at it.
The double.
That's what we have been using here.
And then.
For that, let's import the function.
Here you go.
All set?
You're going to name this one as.
Student function.
Now, we all know the input is going to be students, right?
There you go.
And then we are going to open the curly braces because we are going to do a lot of logic in order to
get to that result.
So the lambda body is going to be more than a single line statement.
That's the reason why we have the curly braces open here.
In here.
The first step is we are going to define a map that's going to be string.
And then it is going to be named as student.
Read map equal to new.
Okay.
Now we are going to.
In that shouldn't and using the for each method what we are going to do.
We are going to iterate each and every student.
And then again, open the lambda body.
You're going to populate the student grade map using the put method.
First is going to be to the name.
Followed by that it is going to have student dot get GPA.
There you go.
The map is going to be populated with the student and their appropriate grades.
Now, I'm going to close this.
I have some issues here.
What is that?
This particular map have to return something, right?
This is going to return the student grade map.
Then student grade map.
So now we have implemented the whole functionality and then assign that functionality to this.
Object reference basically to this object.
Now what I'm going to do, I'm going to call the student function dot apply and use a student database
class and get all the students and.
The result is going to be the map of students and their appropriate grades.
There you go.
It printed the value with the students and their appropriate grades.
Now you are changing your mindset.
And then there is a new request came from the business.
Okay, now we want to filter all the students who are greater than.
Grade three level, meaning we go and take a look at the student.
It has a grade level.
They want the students who are the grade of three and four.
So in that case.
You can just change your functionality.
If you take a look at the predicate student example, we have this right and it is defined in static
context.
So I'm going to make use of that.
I'm going to go back to the function student example in here, what I'm going to do.
And apply this logic predicate example Dot.
Believe it is P1 to go to P1.
P1 is the one which has a grade level greater than or equal to three.
Dot test student.
Now, if you take a look at it, we are filtering the student students who are in the grade of three
and four.
Basically, Adam and Emily, they're not going to be printed in the result.
There you go.
Emily is printed.
I believe Emily is part of the third grade only.
Yep.
The result is printed as expected.
So this is the advantage of using lambdas.
So you can plug in.
New features very easily.
And then and it is more readable.
So that's one of the advantage of using Lambda, which have been highlighting from the beginning.
With this.
We came to the end of this tutorial.
Thank you for watching.