Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the by function functional interface.
The first step is I'm going to search for that by function function interface and then see what is that
all about.
So by function, functional interface is similar to the function interface, but the only difference
is the by function accepts two inputs and then produces one output.
I'm going to open the function functional interface two and then compare these two.
So if we take a look at the function, it has a play method as like the by function method, but it
accepts only one input and one output.
And it has a compost method and another method and it has a static identity method.
But the by function method doesn't have all those methods.
It just has the and then method.
Now let's go ahead and code this.
Scenario using the by function functional interface.
I'm going to create a class called.
By function.
Example.
And then I'm going to make this class executable by adding the public static void main method.
The use case that we are going to code is that my function is going to accept two inputs, right?
First one is going to be the list of students.
The second one we are going to pass a predicate as an input.
So until now, we have never passed a functional interface implementation as an input.
In this example, we are going to explore that and the output is going to be a map of students and their
respective GPAs.
Let's create the instance of by function function, interface by function.
So it is going to take three.
Parameters passed to are going to be the input and third one is going to be the output.
The first one is going to be the list of students.
Riggo Coldset.
And the next one is going to be.
Predicate of student.
So this is the functional interface which we are passing as an input to that by function functional
interface.
Predicate of student hours typo here.
Let's import predicate.
The third one is the output.
What is the input?
We are output.
The output is going to be map of.
Ring and double basically map of the names, which is of type string and their respective GPUs.
GPUs are in.
And I'm going to give a name by function.
So this is going to accept.
The shouldn't list, which is equivalent to students.
And then is going to perform some operation.
The first step is we are going to initialize.
String double.
We are going to create a HashMap.
Student grade map equal to new hash map.
It.
What is the next step?
The next step is we are going to iterate the student list.
Reach.
And we are going to use.
This predicate.
Right if shouldn't predicate.
Dot test student.
Then add those student in the student grade map dot put.
Didn't dot get name comma didn't dot get.
And then we have to return this hash map.
The next thing, return the student grade map.
Not a semicolon here.
There you go.
We have the by function functional interface implementation ready.
The next step is we are going to pass some input to it.
So let's create the System.out.println.
In here.
What we are going to do, we are going to do by function dot apply.
We take a look at it.
It takes two input parameters.
So using the student database class.
Database, you're going to get all the list of students and then we have to pass a predicate.
Let's go and check this predicate.
Student example.
I'm going to use this predicate in order to filter the.
Students.
So I'm going to use predicate example.
Pterygoids.
F stands for Functional interface.
We're going to use P1.
Basically.
What is that it is going to do?
It is going to filter the students based on their grade level.
So it is going to produce the output with all the students who are the grade level greater than or equal
to three.
Then I'm going to run this example.
There you go.
It gave all the students whose grade level is greater than or equal to three.
Let's say you change your mind and then you want to use this another functional interface predicate
implementation, which has this filter which is equivalent to the GPA greater than or equal to 3.9.
I'm going to run this.
Is going to produce a result accordingly.
Now it is only three results in the output because only three results from this election matches the
filter.
Basically, who are having the grade, which is the GPA greater than or equal to 3.9.
With this.
We came to the end of this tutorial.
Thank you for watching.