Hi Everyone will go back to this tutorial. In this tutorial will implement the comparator interface using
Lambda.
Let's go ahead and code this one. First step is that I'm going to create a class called ComparatorLambdaExample.
And then I'm going to make this class executable. The way to make this class executable is by adding
the public static void main(). There you go!
We have the class ready to run.
Before we code this one let's check
what is the abstract method the comparator class has and what is the purpose it ? I am going to press the shift
key twice.
It is giving me this window search everywhere.
Then I'm going to search for Comparator. Search for Comparator
I have a lot of different classes here the one which I'm going to take a look at is the one which has which
is having the icon as "I" stands for interface . Because that is the one which we are going to implement
now.
Select this one.
OK.
If you take a look at it it has this @FunctionalInterface
annotation meaning this is a Function Interface.
The next step in the process is that if you take a look at it it is available in Java
JDK since 1.2. And what is abstract method that it has ?
It has the method called compare().
And the purpose of that one is to compare two inputs of any type.
Could be Integer, Double , Float, String or etc., and the idea is to write your own logic on how
you want to compare the inputs in your implementation.
Lets scroll down a bit and check what are the different things that this interface has.
There are some other method with the default keyword.
You might be wondering that the definition of functional interface is just to have one abstract method.
But I see other methods too here.
Is it not breaking the contract of a Functional Interface?
The answer is No.
default() methods are something which got introduced as part of Java 1.8 . I'll be covering this topic as
a separate section in this course.
Now let's not worry about the default keyword.
Let's go ahead and implement this Comparator interface.
Lets write the code in implementing the Comparator interface the prior Java 8 way and then the lambda approach.
Let me give some comment here prior Java 8.
And then give Comparator.
I'm going to compare two Integers part of this Comparator.
Let me give a name and then the prior java is by creating the anonymous classes.
Create Comparator instance. There you go ! The Comparator's compare method is here. Now we will write the logic
in here.
What I am going to do is I'm going to compare I am going to use the compareTo() method which
is part of the Integer.
Lets write the logic in here.
If both the values are the same then it is going to return zero which is o1 == o2
o1 == o2;
Next step is if o1 > o2 then it is going to return 1. Next is -1
If o1 < o2. This is what this compareTo()
is going to do.
Now let's call this anonymous class implementation. How do we do it ? Am gonna have
Result of the comparator is
here we are going to print the result.
I'm going to call comparator and it is going to give the recommendation of compare(). What is the input that
we're going to pass ? I'm going to pass (3,2). Lets run this!
So the result of the comparator is 1.
Until now there is nothing new here.
It's just the old way. Most of the developers who have worked using Comparator they would have
Implemented it this way. Now lets implement it using the Lambda way. Comparator and
am going to give it a name called comparatorLambda = ()
If you take a look at the Comparator interface it accepts two inputs, I am going to pass those (Integer a, Integer b)
(Integer a, Integer b) -> followed by that lets put in the arrow. The syntax for
the lambda body is by giving the curly braces.
Right. Now what we will do we will do a.compareTo(b)
But there is some compilation issue! I think the result is have to return this right ?
Lets take a look at the issues that we're facing here is expecting this ";" correct.
Ideally if you take a look at it
it is just single statement right ?
If you remember from the previous tutorial. If it is a single statement
we don't even need this.
Thats it!
So now we have implemented comparator interface using Lambda. Am gonna call this one by copying the same.
"Result of the Comparator using Lambda is :" In here we are going to use the comparator. Lets run this.
So we have successfully implemented it. The results
if you take a look at it both are the same and we have successfully implemented the Comparator interface
using lambda.
Next up is here we are explicitly mentioning the type.
Ideally you don't have to do it because here you have already mentioned that type.
that this particular lambda expression or this Comparator interface is going accept.
So in those kind of scenarios we don't even have to provide the type explicitly there.
I'm going to name this one as comparatorLambda1. Lambda is really really smart enough to know to
type cast this input to integer.
So, I'm gonna run this.
And then I am going to use the comparatorlambda1 instance and run this.
Result is 1. As part of the tutorial we have explored the lambda expression that accepts
an input and returns an output.
Advantages are clearly evident if you compared this syntax with this syntax. Lambda lets you write more concise
and clear code.
It might be difficult to adapt in the beginning,
once you get the hang of the lambda expression. You will love it! I will cover more about Lambdas and its advantages in
the upcoming tutorials.
With this we came to the end of the territorial.
Thank you for watching.