WEBVTT

00:00.930 --> 00:01.470
Hi everyone.

00:01.470 --> 00:02.700
Welcome back to this tutorial.

00:02.700 --> 00:08.990
In this tutorial we will code and explore the unary and binary operator Functional interface.

00:09.000 --> 00:13.590
Let's go ahead and look into the unary operator functional interface.

00:14.760 --> 00:19.050
I am pressing the shift key twice, which is giving me the search everywhere option I'm going to search

00:19.050 --> 00:21.150
for unary operator.

00:21.540 --> 00:26.280
The unary operator is part of the Java.util dot function.

00:27.270 --> 00:28.020
Package.

00:28.200 --> 00:29.010
Now click on that.

00:29.310 --> 00:30.170
We take a look at it.

00:30.180 --> 00:33.150
This got introduced as part of Java eight.

00:33.150 --> 00:40.740
And then this is a functional interface, this functional interface extend the function functional interface.

00:40.740 --> 00:46.980
We discussed about function functional interface in one of our previous tutorial, a function interface

00:46.980 --> 00:51.420
which takes an input of one type and produces an output of other type.

00:51.870 --> 00:55.340
Let's go ahead and take a look at one of the example that we coded before.

00:55.350 --> 01:01.110
So if you take a look at the function student example here in this function student example, we have

01:01.110 --> 01:10.830
the input as list of students, but it returns a map of student of type of key string and then the value

01:10.830 --> 01:12.000
as double.

01:12.740 --> 01:16.190
Now, you might be having this question in your mind.

01:16.220 --> 01:19.360
So we have a function interface already.

01:19.370 --> 01:22.730
So under what scenarios I will use unary operator.

01:23.000 --> 01:27.470
Let's say you have a use case where the input and output are of the same type.

01:27.770 --> 01:33.800
Those use cases you can use unary operator functional interface instead of a function.

01:33.830 --> 01:37.730
Now let's go ahead and create the unary operator.

01:38.730 --> 01:39.420
Example.

01:42.000 --> 01:43.350
Unary operator.

01:44.460 --> 01:44.970
Temple.

01:47.510 --> 01:49.360
I'm going to make this class executable.

01:49.390 --> 01:50.980
Public static void Main.

01:52.700 --> 01:59.930
What I'm going to do is I'm going to declare a unary operator, unary operator, and it is going to

02:00.290 --> 02:02.450
take in a type called string.

02:03.290 --> 02:08.570
So basically the input and output of this unary operator is going to be string.

02:09.910 --> 02:11.050
The next steps.

02:12.680 --> 02:14.330
This string is going to.

02:15.910 --> 02:18.820
Perform a concat operation.

02:20.050 --> 02:21.490
Though I didn't give the.

02:23.430 --> 02:25.350
Variable name is Dot.

02:27.110 --> 02:30.800
Venkat is going to add a default string to it.

02:32.520 --> 02:33.050
That's it.

02:33.060 --> 02:35.090
I want to keep this example really simple.

02:35.100 --> 02:41.430
So in here, this unary operator is going to take in a type as string and it is going to return the

02:41.430 --> 02:42.960
type as string.

02:43.480 --> 02:44.400
Is that one?

02:46.710 --> 02:49.180
Re operator dot apply.

02:50.240 --> 02:52.430
When a pass a string as Java eight.

02:57.760 --> 03:01.450
So here it printed the value as Java eight default.

03:01.780 --> 03:07.030
Basically, it successfully applied this concat operation to the input that we passed.

03:07.120 --> 03:09.340
So this is all about unary operator.

03:09.610 --> 03:10.930
So keep that in your mind.

03:10.930 --> 03:17.200
Whenever you have a use case where the input and output are of the same type in those kind of use cases,

03:17.200 --> 03:19.090
you don't even have to use a function.

03:19.090 --> 03:22.950
You can use unary operator in those use cases.

03:22.960 --> 03:25.450
Let's talk about binary operator now.

03:26.490 --> 03:29.370
On a search for binary operator.

03:30.140 --> 03:35.690
The binary operator is also part of the java.util dot function package.

03:38.190 --> 03:43.980
From the name, we can say that it is going to act on two inputs and it is going to produce one output.

03:44.010 --> 03:50.020
So if you take a look at the binary operator, it extends the by function functional interface.

03:50.040 --> 03:52.480
So what is a by function functional interface?

03:52.500 --> 03:55.680
It takes in two input and then produces one output.

03:55.710 --> 03:59.280
We have coded the by function in one of the previous tutorial.

03:59.280 --> 04:01.110
Let's go ahead and take a look at that.

04:01.560 --> 04:02.850
Take a look at the by function.

04:02.850 --> 04:09.630
It takes a list of student and then it takes the second input as a predicate and it produces the map

04:09.630 --> 04:10.740
as an output.

04:12.130 --> 04:16.120
Go back to the binary operator example under what scenarios you will use.

04:16.120 --> 04:16.960
Binary Operator.

04:16.960 --> 04:19.270
That might be the question that will be running in your mind.

04:19.300 --> 04:26.470
So if the inputs and the output are of the same type, then in those kind of use cases we will use binary

04:26.470 --> 04:32.980
operator, binary operator as two additional static methods which are mean by and max by.

04:33.010 --> 04:36.310
Let's go ahead and code and explore all these methods.

04:40.400 --> 04:40.730
A step.

04:40.730 --> 04:45.170
I'm going to create the class called Binary operator.

04:46.180 --> 04:52.270
Example, want to make this class executable by adding the public static void main method.

04:52.780 --> 04:58.600
The next step is we are going to create the implementation for binary operator.

04:58.990 --> 05:01.540
I want to create a really simple use case.

05:01.570 --> 05:07.390
It's going to take in two inputs and then perform the multiplication operation on that and return the

05:07.390 --> 05:08.050
result.

05:08.390 --> 05:09.670
So integer.

05:10.800 --> 05:12.870
Next up is binary operator.

05:13.080 --> 05:16.890
Given a name now, it's going to take in two inputs.

05:18.470 --> 05:25.160
And then it produces one output, which is the multiplication of those two inputs.

05:26.810 --> 05:28.190
When does this?

05:29.240 --> 05:31.820
Binary operator does not apply.

05:32.670 --> 05:35.880
Pick come up for, though the result is going to be 12.

05:37.590 --> 05:38.170
There we go.

05:38.190 --> 05:41.550
We have successfully implemented the binary operator.

05:41.700 --> 05:45.060
Now let's take a look at some of the static methods that it has.

05:45.090 --> 05:46.860
One is the min by and max by.

05:49.480 --> 05:52.630
So let's explore and discuss more about it.

05:52.660 --> 05:57.400
The min boy accepts a comparator and the Max boy accepts a comparator.

05:57.580 --> 05:59.220
What we have to do is the first step.

05:59.260 --> 06:01.480
We need to create an instance of comparator.

06:01.480 --> 06:03.610
I'm going to create a comparator.

06:03.670 --> 06:04.360
The class level.

06:04.360 --> 06:09.430
I'm going to declare that one is in the static keyword because I'm going to use it in our.

06:10.340 --> 06:10.940
Main method.

06:11.510 --> 06:14.430
So in here it is going to take an integer.

06:14.450 --> 06:21.770
What this comparator is going to do, it is going to use the Compareto method, which is part of the

06:22.280 --> 06:23.450
integer class.

06:23.720 --> 06:29.600
It's going to accept to input a comma B because comparator requires two input and then it is going to

06:29.600 --> 06:32.360
perform a dot compare to B.

06:32.390 --> 06:34.790
So this is what is going to do.

06:34.820 --> 06:40.700
If we take a look at the comparator method, it compares the two integers which are passed to it and

06:40.700 --> 06:42.650
then it returns an integer again.

06:43.720 --> 06:44.140
Okay.

06:44.380 --> 06:45.190
What's the next step?

06:45.220 --> 06:50.320
The next step is I'm going to create one more instance of binary operator.

06:51.950 --> 06:57.080
And then I'm going to name this one as Max Buy because we are going to test the Max buy method as part

06:57.080 --> 06:57.740
of this one.

06:57.890 --> 07:00.290
So in here, the static method, right?

07:00.290 --> 07:06.020
So we will call it using using the binary operator class Max buy and then pass the comparator.

07:07.760 --> 07:11.510
Okay, so it should be comparator.

07:14.920 --> 07:15.430
The next step.

07:15.430 --> 07:17.710
How do we pass inputs to it?

07:17.740 --> 07:22.960
The way to pass the input is result of max by is.

07:24.710 --> 07:29.420
Bags by dot apply and pass the two integers.

07:29.420 --> 07:30.440
Four comma five.

07:30.830 --> 07:34.100
So since it's a max by function, it is going to return.

07:34.100 --> 07:35.150
What is the.

07:36.610 --> 07:38.740
Largest meaning greatest value out of the two.

07:40.500 --> 07:45.240
It printed the value as five because five is greater than four.

07:45.270 --> 07:47.610
Next we will implement the min.

07:47.610 --> 07:52.230
By that I'm going to name this one as min by.

07:54.500 --> 08:02.600
Then I'm going to name change the result text also to mean by and I'm going to use min by method here.

08:03.550 --> 08:04.020
There you go.

08:04.030 --> 08:06.400
If I run this, it is going to print.

08:07.440 --> 08:08.310
Paul over here.

08:11.420 --> 08:14.330
So here I'm still using the max buy dot apply.

08:15.450 --> 08:16.830
A copy paste error.

08:18.130 --> 08:18.970
This again.

08:20.490 --> 08:20.930
There you go.

08:20.940 --> 08:22.170
The result is four.

08:22.290 --> 08:26.900
So this is all about unary operator and binary operator.

08:26.910 --> 08:33.810
If the input and output types are the same, then we can use the binary operator and unary operator

08:33.840 --> 08:35.580
the place of function.

08:35.700 --> 08:37.200
With this we came to the end of this.
