WEBVTT

00:00.290 --> 00:00.860
Hi everyone.

00:00.860 --> 00:02.090
Welcome back to this tutorial.

00:02.090 --> 00:07.580
In this tutorial we will code and explore the function interface, which is part of the Java.util dot

00:07.640 --> 00:08.480
package.

00:08.480 --> 00:12.470
From the name it is clear that it stands for function.

00:12.500 --> 00:17.510
We all know that function in Java is nothing but a method in a class.

00:17.510 --> 00:26.330
So using function interface we can implement a functionality as like we code a method in Java and assign

00:26.330 --> 00:29.000
that functionality to a variable.

00:29.030 --> 00:35.090
This concept is really new in Java as a whole and this was never possible before.

00:35.090 --> 00:35.960
Java eight.

00:36.980 --> 00:43.190
Think about it again, coding a piece of logic and assigning it a variable and pass it across like any

00:43.190 --> 00:45.260
other object or variable.

00:45.260 --> 00:48.080
This is an excellent improvement in Java.

00:48.110 --> 00:51.140
Let's go ahead and explore that function interface.

00:53.650 --> 00:56.350
Step is I'm going to search for that function interface.

01:00.910 --> 01:07.270
And then if you take a look at the function interface, this got introduced as part of Java 1.8 and

01:07.270 --> 01:13.900
this one is part of the Java.util dot function package and it has one abstract method which is apply

01:13.900 --> 01:21.550
and it has two default methods, which is compose and and then now let's go ahead and explore this.

01:24.580 --> 01:30.010
Suppose I'm going to create a class called function example.

01:31.970 --> 01:35.960
And then I'm going to make this class executable.

01:35.990 --> 01:36.590
There we go.

01:36.620 --> 01:37.820
The class is ready.

01:37.850 --> 01:42.830
Now what I'm going to do, I'm going to create static.

01:44.290 --> 01:45.040
Function.

01:45.520 --> 01:50.920
So this function takes in an input and returns an output.

01:51.280 --> 01:53.770
So if you go ahead and open this function.

01:55.630 --> 01:56.620
Interface.

01:56.800 --> 01:58.690
It has one method called apply.

01:58.990 --> 02:02.020
It takes an input and it returns an output.

02:02.050 --> 02:09.260
But you have to pass two different types when you define the function interface.

02:09.290 --> 02:16.060
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

02:16.060 --> 02:17.230
also as a string.

02:17.900 --> 02:18.310
Okay.

02:18.340 --> 02:20.020
And then I'm going to assign this.

02:22.120 --> 02:24.940
What this particular function is going to do.

02:24.940 --> 02:27.010
I'm going to keep things really simple.

02:27.370 --> 02:28.810
This is going to.

02:30.470 --> 02:33.770
Your name and then it is going to perform.

02:35.700 --> 02:37.890
The uppercase operation on that string.

02:37.890 --> 02:44.070
So basically the input is going to be a string and then output is also going to be a string, but the

02:44.070 --> 02:51.570
output is going to be the uppercase version of the input that is being passed to this function interface.

02:51.690 --> 02:54.300
Now what I'm going to do, I'm going to use this.

02:56.250 --> 02:59.790
Result is what I'm going to do.

02:59.820 --> 03:02.430
I'm going to apply some input to it.

03:02.460 --> 03:10.800
So the method to use in order to pass the input to that particular function interface here is apply.

03:11.040 --> 03:13.050
Here we are going to pass Java eight.

03:13.470 --> 03:14.750
So what will be the output?

03:14.760 --> 03:19.140
The output is it is going to be the uppercase version of Java eight.

03:19.380 --> 03:21.480
As you see, the result is Java eight.

03:21.660 --> 03:28.950
Now what I'm going to do, I'm going to create one more function interface implementation, and then

03:28.950 --> 03:33.810
let's explore the and then method and compose method, which is part of the function interface.

03:33.810 --> 03:37.500
If we go and take a look at the function interface, there are two default methods.

03:37.530 --> 03:39.540
One is compose and next one is.

03:39.570 --> 03:40.200
And then.

03:41.790 --> 03:46.620
For that, I'm going to create one more instance of function interface.

03:46.620 --> 03:49.920
I'm going to name this one as add some string.

03:50.400 --> 03:51.600
Why do I name it like that?

03:51.600 --> 03:54.930
This is going to concat some string.

03:57.120 --> 04:00.150
So I'm going to name it as add some string.

04:00.390 --> 04:06.420
Basically, this is going to perform the uppercase operation and then add this default string to it.

04:08.170 --> 04:11.170
Now let's go ahead and explore the and then method.

04:13.030 --> 04:14.020
Copy this.

04:15.370 --> 04:18.670
Result of and then is.

04:20.010 --> 04:20.310
Here.

04:20.310 --> 04:26.640
I'm going to use the and then pass the add substring instance, then dot apply.

04:27.030 --> 04:31.770
Now if we take a look at it, it's going to get the input as Java eight.

04:31.980 --> 04:37.770
First it is going to perform the uppercase operation and then the result of that is going to be passed

04:37.770 --> 04:44.550
to this particular add some string function that is again going to perform the uppercase operation and

04:44.550 --> 04:49.050
then concat the result and print it in the console and run this.

04:51.730 --> 04:56.410
See it performed the uppercase operation first.

04:56.650 --> 05:01.450
That is the reason why you see this Java eight as a same string in here.

05:01.450 --> 05:05.570
And then the result of that is being passed to this function.

05:05.590 --> 05:12.610
Again, the uppercase operation is performed on the result of the first function result and then it

05:12.610 --> 05:16.270
added the default string to that one.

05:16.300 --> 05:18.340
Now let's explore.

05:21.110 --> 05:21.920
The compost method.

05:24.220 --> 05:24.730
Right.

05:24.730 --> 05:28.840
So this compost method, what it is going to do, it is going to.

05:30.400 --> 05:32.560
Execute this operation first.

05:32.590 --> 05:40.540
Basically, it is going to perform the uppercase operation and then it is going to concat the default

05:40.690 --> 05:43.960
and the result of that will be passed to this one.

05:44.050 --> 05:46.360
So the output is going to be.

05:48.010 --> 05:52.390
Java eight default, but everything will be in uppercase.

05:52.810 --> 05:54.190
Let's go ahead and execute this.

05:55.540 --> 05:56.380
There you go.

05:56.590 --> 06:05.080
So the difference between and then and compose is that and then executes the functions and the order

06:05.080 --> 06:06.100
it is mentioned.

06:06.550 --> 06:14.680
Compose is something which executes the first parameter function that is being passed to this compose.

06:15.520 --> 06:17.710
And then it executes the outer function.

06:18.430 --> 06:22.420
So that is some difference which you have to understand.

06:22.610 --> 06:26.650
So you can chain n number of and then s and n number of.

06:27.090 --> 06:29.620
I can do and then and pass another function.

06:30.650 --> 06:32.480
And I can do compose.

06:33.710 --> 06:36.690
You can do any number of and then and compose as part of it.

06:36.700 --> 06:38.470
And this concept is called.

06:40.180 --> 06:40.810
Chaining.

06:41.930 --> 06:44.450
Now, what are the advantages of this?

06:44.450 --> 06:44.720
Right?

06:44.750 --> 06:49.880
Now, I have this piece of functionality defined here, and if I want to use this functionality somewhere

06:49.880 --> 06:50.840
else in my code.

06:51.140 --> 06:58.400
Now to demonstrate that what I'm going to do, I'm going to create another class called function Example

06:58.400 --> 06:58.970
one.

06:59.750 --> 07:00.260
Okay.

07:00.260 --> 07:05.690
And then this is going to have a method called public static void main.

07:05.690 --> 07:07.100
And in here.

07:08.920 --> 07:14.140
I have the I have a method called public static void.

07:18.150 --> 07:18.570
String.

07:18.570 --> 07:20.460
So this method accepts a input called.

07:21.640 --> 07:23.280
String here.

07:26.120 --> 07:27.090
I didn't give the method.

07:27.090 --> 07:27.750
Name it.

07:31.310 --> 07:34.220
Concat it is going to perform the concat.

07:36.550 --> 07:38.260
And I'm going to call this.

07:40.620 --> 07:42.270
And parse a string called Hello.

07:45.980 --> 07:51.260
Now we want to concat the same default string to that one.

07:51.410 --> 07:56.930
In those kind of scenarios what you can do is since that particular.

07:58.810 --> 08:01.450
Function is defined in the static context.

08:01.990 --> 08:06.760
Gonna use that and then.

08:08.980 --> 08:14.140
Apply str the string that we are passing to this method.

08:17.090 --> 08:18.500
But this one returns a.

08:19.760 --> 08:20.570
String rate.

08:20.960 --> 08:23.600
String result equal to this.

08:25.270 --> 08:25.630
Perform.

08:25.630 --> 08:27.640
Concat is going to return that.

08:30.760 --> 08:32.280
Want to change our return type.

08:32.490 --> 08:34.830
Now we are going to print the result.

08:40.800 --> 08:42.750
Result and print the result.

08:47.130 --> 08:48.240
Now let's run it.

08:49.560 --> 08:49.980
There you go.

08:50.730 --> 08:52.200
We have hello.

08:52.200 --> 08:58.590
And then the default string added to that input that we have passed.

08:58.740 --> 09:01.950
So this is one of the benefit reusability again.

09:01.950 --> 09:05.550
So lambda expression is meant for reusability.

09:05.550 --> 09:08.070
And then one more thing is that.

09:09.490 --> 09:12.010
It's more readable, the code is more readable.

09:12.010 --> 09:17.470
And then you can, if you want to modify this or if you want to add some other functionality, then

09:17.470 --> 09:21.910
you can create a new function interface and you can reuse it throughout the code.

09:23.230 --> 09:25.120
So this we came to the end of this tutorial.

09:25.120 --> 09:31.240
In the next tutorial, we will explore the real world use case using the function interface.
