Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the function interface, which is part of the Java.util dot
package.
From the name it is clear that it stands for function.
We all know that function in Java is nothing but a method in a class.
So using function interface we can implement a functionality as like we code a method in Java and assign
that functionality to a variable.
This concept is really new in Java as a whole and this was never possible before.
Java eight.
Think about it again, coding a piece of logic and assigning it a variable and pass it across like any
other object or variable.
This is an excellent improvement in Java.
Let's go ahead and explore that function interface.
Step is I'm going to search for that function interface.
And then if you take a look at the function interface, this got introduced as part of Java 1.8 and
this one is part of the Java.util dot function package and it has one abstract method which is apply
and it has two default methods, which is compose and and then now let's go ahead and explore this.
Suppose I'm going to create a class called function example.
And then I'm going to make this class executable.
There we go.
The class is ready.
Now what I'm going to do, I'm going to create static.
Function.
So this function takes in an input and returns an output.
So if you go ahead and open this function.
Interface.
It has one method called apply.
It takes an input and it returns an output.
But you have to pass two different types when you define the function interface.
So what I'm going to do, I'm going to pass the input as a string and then I'm going to get the output
also as a string.
Okay.
And then I'm going to assign this.
What this particular function is going to do.
I'm going to keep things really simple.
This is going to.
Your name and then it is going to perform.
The uppercase operation on that string.
So basically the input is going to be a string and then output is also going to be a string, but the
output is going to be the uppercase version of the input that is being passed to this function interface.
Now what I'm going to do, I'm going to use this.
Result is what I'm going to do.
I'm going to apply some input to it.
So the method to use in order to pass the input to that particular function interface here is apply.
Here we are going to pass Java eight.
So what will be the output?
The output is it is going to be the uppercase version of Java eight.
As you see, the result is Java eight.
Now what I'm going to do, I'm going to create one more function interface implementation, and then
let's explore the and then method and compose method, which is part of the function interface.
If we go and take a look at the function interface, there are two default methods.
One is compose and next one is.
And then.
For that, I'm going to create one more instance of function interface.
I'm going to name this one as add some string.
Why do I name it like that?
This is going to concat some string.
So I'm going to name it as add some string.
Basically, this is going to perform the uppercase operation and then add this default string to it.
Now let's go ahead and explore the and then method.
Copy this.
Result of and then is.
Here.
I'm going to use the and then pass the add substring instance, then dot apply.
Now if we take a look at it, it's going to get the input as Java eight.
First it is going to perform the uppercase operation and then the result of that is going to be passed
to this particular add some string function that is again going to perform the uppercase operation and
then concat the result and print it in the console and run this.
See it performed the uppercase operation first.
That is the reason why you see this Java eight as a same string in here.
And then the result of that is being passed to this function.
Again, the uppercase operation is performed on the result of the first function result and then it
added the default string to that one.
Now let's explore.
The compost method.
Right.
So this compost method, what it is going to do, it is going to.
Execute this operation first.
Basically, it is going to perform the uppercase operation and then it is going to concat the default
and the result of that will be passed to this one.
So the output is going to be.
Java eight default, but everything will be in uppercase.
Let's go ahead and execute this.
There you go.
So the difference between and then and compose is that and then executes the functions and the order
it is mentioned.
Compose is something which executes the first parameter function that is being passed to this compose.
And then it executes the outer function.
So that is some difference which you have to understand.
So you can chain n number of and then s and n number of.
I can do and then and pass another function.
And I can do compose.
You can do any number of and then and compose as part of it.
And this concept is called.
Chaining.
Now, what are the advantages of this?
Right?
Now, I have this piece of functionality defined here, and if I want to use this functionality somewhere
else in my code.
Now to demonstrate that what I'm going to do, I'm going to create another class called function Example
one.
Okay.
And then this is going to have a method called public static void main.
And in here.
I have the I have a method called public static void.
String.
So this method accepts a input called.
String here.
I didn't give the method.
Name it.
Concat it is going to perform the concat.
And I'm going to call this.
And parse a string called Hello.
Now we want to concat the same default string to that one.
In those kind of scenarios what you can do is since that particular.
Function is defined in the static context.
Gonna use that and then.
Apply str the string that we are passing to this method.
But this one returns a.
String rate.
String result equal to this.
Perform.
Concat is going to return that.
Want to change our return type.
Now we are going to print the result.
Result and print the result.
Now let's run it.
There you go.
We have hello.
And then the default string added to that input that we have passed.
So this is one of the benefit reusability again.
So lambda expression is meant for reusability.
And then one more thing is that.
It's more readable, the code is more readable.
And then you can, if you want to modify this or if you want to add some other functionality, then
you can create a new function interface and you can reuse it throughout the code.
So this we came to the end of this tutorial.
In the next tutorial, we will explore the real world use case using the function interface.