Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the unary and binary operator Functional interface.
Let's go ahead and look into the unary operator functional interface.
I am pressing the shift key twice, which is giving me the search everywhere option I'm going to search
for unary operator.
The unary operator is part of the Java.util dot function.
Package.
Now click on that.
We take a look at it.
This got introduced as part of Java eight.
And then this is a functional interface, this functional interface extend the function functional interface.
We discussed about function functional interface in one of our previous tutorial, a function interface
which takes an input of one type and produces an output of other type.
Let's go ahead and take a look at one of the example that we coded before.
So if you take a look at the function student example here in this function student example, we have
the input as list of students, but it returns a map of student of type of key string and then the value
as double.
Now, you might be having this question in your mind.
So we have a function interface already.
So under what scenarios I will use unary operator.
Let's say you have a use case where the input and output are of the same type.
Those use cases you can use unary operator functional interface instead of a function.
Now let's go ahead and create the unary operator.
Example.
Unary operator.
Temple.
I'm going to make this class executable.
Public static void Main.
What I'm going to do is I'm going to declare a unary operator, unary operator, and it is going to
take in a type called string.
So basically the input and output of this unary operator is going to be string.
The next steps.
This string is going to.
Perform a concat operation.
Though I didn't give the.
Variable name is Dot.
Venkat is going to add a default string to it.
That's it.
I want to keep this example really simple.
So in here, this unary operator is going to take in a type as string and it is going to return the
type as string.
Is that one?
Re operator dot apply.
When a pass a string as Java eight.
So here it printed the value as Java eight default.
Basically, it successfully applied this concat operation to the input that we passed.
So this is all about unary operator.
So keep that in your mind.
Whenever you have a use case where the input and output are of the same type in those kind of use cases,
you don't even have to use a function.
You can use unary operator in those use cases.
Let's talk about binary operator now.
On a search for binary operator.
The binary operator is also part of the java.util dot function package.
From the name, we can say that it is going to act on two inputs and it is going to produce one output.
So if you take a look at the binary operator, it extends the by function functional interface.
So what is a by function functional interface?
It takes in two input and then produces one output.
We have coded the by function in one of the previous tutorial.
Let's go ahead and take a look at that.
Take a look at the by function.
It takes a list of student and then it takes the second input as a predicate and it produces the map
as an output.
Go back to the binary operator example under what scenarios you will use.
Binary Operator.
That might be the question that will be running in your mind.
So if the inputs and the output are of the same type, then in those kind of use cases we will use binary
operator, binary operator as two additional static methods which are mean by and max by.
Let's go ahead and code and explore all these methods.
A step.
I'm going to create the class called Binary operator.
Example, want to make this class executable by adding the public static void main method.
The next step is we are going to create the implementation for binary operator.
I want to create a really simple use case.
It's going to take in two inputs and then perform the multiplication operation on that and return the
result.
So integer.
Next up is binary operator.
Given a name now, it's going to take in two inputs.
And then it produces one output, which is the multiplication of those two inputs.
When does this?
Binary operator does not apply.
Pick come up for, though the result is going to be 12.
There we go.
We have successfully implemented the binary operator.
Now let's take a look at some of the static methods that it has.
One is the min by and max by.
So let's explore and discuss more about it.
The min boy accepts a comparator and the Max boy accepts a comparator.
What we have to do is the first step.
We need to create an instance of comparator.
I'm going to create a comparator.
The class level.
I'm going to declare that one is in the static keyword because I'm going to use it in our.
Main method.
So in here it is going to take an integer.
What this comparator is going to do, it is going to use the Compareto method, which is part of the
integer class.
It's going to accept to input a comma B because comparator requires two input and then it is going to
perform a dot compare to B.
So this is what is going to do.
If we take a look at the comparator method, it compares the two integers which are passed to it and
then it returns an integer again.
Okay.
What's the next step?
The next step is I'm going to create one more instance of binary operator.
And then I'm going to name this one as Max Buy because we are going to test the Max buy method as part
of this one.
So in here, the static method, right?
So we will call it using using the binary operator class Max buy and then pass the comparator.
Okay, so it should be comparator.
The next step.
How do we pass inputs to it?
The way to pass the input is result of max by is.
Bags by dot apply and pass the two integers.
Four comma five.
So since it's a max by function, it is going to return.
What is the.
Largest meaning greatest value out of the two.
It printed the value as five because five is greater than four.
Next we will implement the min.
By that I'm going to name this one as min by.
Then I'm going to name change the result text also to mean by and I'm going to use min by method here.
There you go.
If I run this, it is going to print.
Paul over here.
So here I'm still using the max buy dot apply.
A copy paste error.
This again.
There you go.
The result is four.
So this is all about unary operator and binary operator.
If the input and output types are the same, then we can use the binary operator and unary operator
the place of function.
With this we came to the end of this.