Hi everyone.
Welcome back to this tutorial.
In this tutorial we will talk about supplier functional interface.
Let's explore and code this one.
First up, I'm going to search for the supplier functional interface and it is part of the Java.util
dot function package.
Select that.
You take a look at it.
It got introduced as part of Java eight and it has the functional interface annotation.
This particular functional interface has just one method called get, and it returns something to the
caller.
So basically it is going to return a type to the caller.
This one is exactly opposite to the consumer function interface.
Compare this to search for consumer.
The consumer is again part of the Java.util dot function package.
We take a look at the consumer.
It accepts an input and it doesn't return any output.
But supplier doesn't take any input, but it returns an output.
Let's go ahead and code this one.
A player.
I make this class executable?
Add public static, void main.
There you go.
And then let's create an instance of a player.
A player.
So it is going to return a student object.
As part of this one, I'm going to name this one as student sub player.
So what it is going to do is it is going to return a student, Right.
So it doesn't take any input.
And then we're going to have it like this.
It is going to return a student, new student.
So if you take a look at the student, it accepts name, grade level GPA, gender and list of activities.
So I'm going to open I'm going to expand this data package.
It's going to have the student database.
I'm going to copy one of these.
And then.
Senate under.
Then statement.
There you go.
So this one.
Is going to return a student.
Right.
Let's call this one.
How do we invoke the supplier?
Thus I'm going to call student supplier as soon as I press the I mean, as soon as I enter that it is
going to give me this get method that this get method is going to return the student that is as part
of the student supplier.
Function interface.
Now let's take a look at one more example.
Go to the student database.
Here we are having a static method.
This static method is returning the list of students.
How can we achieve the same thing using the supplier?
So let's create one more instance of supplier.
The player is going to take the input as meaning the type as list of students.
And then.
Give it a name.
The supplier.
This is going to.
Of something like.
It doesn't take an input.
Student database dot get all students.
So this student supplier instance, meaning the list of player instance is going to return you a list
of students instead of a single student that we coded just a moment ago.
Now I'm going to call this.
Students are the colon.
Then list supplier dot.
I run this example.
See here it printed all the students.
Let's summarize this one supplier doesn't take any input, but when it's invoked, it is going to supply
something to the caller.
With this, we came to the end of this tutorial.
Thank you for watching.