WEBVTT

0
00:00.180 --> 00:05.430
Hi Everyone will go back to this tutorial. In this tutorial will implement the comparator interface using

1
00:05.430 --> 00:06.320
Lambda.

2
00:06.750 --> 00:17.280
Let's go ahead and code this one. First step is that I'm going to create a class called ComparatorLambdaExample.

3
00:19.050 --> 00:24.720
And then I'm going to make this class executable. The way to make this class executable is by adding

4
00:24.740 --> 00:27.330
the public static void main(). There you go!

5
00:27.330 --> 00:30.750
We have the class ready to run.

6
00:30.930 --> 00:32.730
Before we code this one let's check

7
00:32.730 --> 00:38.820
what is the abstract method the comparator class has and  what is the purpose it ? I am going to press the shift

8
00:38.820 --> 00:39.540
key twice.

9
00:39.570 --> 00:41.930
It is giving me this window search everywhere.

10
00:42.160 --> 00:45.870
Then I'm going to search for Comparator. Search for Comparator

11
00:45.870 --> 00:51.000
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

12
00:51.000 --> 00:56.940
is having the icon as "I" stands for interface . Because that is the one which we are going to implement

13
00:56.940 --> 00:57.370
now.

14
00:57.920 --> 00:59.120
Select this one.

15
01:03.190 --> 01:03.660
OK.

16
01:03.800 --> 01:08.210
If you take a look at it it has this @FunctionalInterface

17
01:08.830 --> 01:11.500
annotation meaning this is a Function Interface.

18
01:11.660 --> 01:19.230
The next step in the process is that if you take a look at it it is available in Java

19
01:19.230 --> 01:25.680
JDK since 1.2. And what is abstract method that it has ?

20
01:25.680 --> 01:28.040
It has the method called compare().

21
01:28.140 --> 01:32.330
And the purpose of that one is to compare two inputs of any type.

22
01:32.360 --> 01:39.900
Could be Integer, Double , Float, String or etc.,  and the idea is to write your own logic on how

23
01:40.140 --> 01:43.740
you want to compare the inputs in your implementation.

24
01:43.820 --> 01:48.320
Lets scroll down a bit and check what are the different things that this interface has.

25
01:48.330 --> 01:51.950
There are some other method with the default keyword.

26
01:52.170 --> 01:57.200
You might be wondering that the definition of functional interface is just to have one abstract method.

27
01:57.220 --> 01:59.470
But I see other methods too here.

28
01:59.490 --> 02:02.910
Is it not breaking the contract of a Functional Interface?

29
02:02.910 --> 02:04.300
The answer is No.

30
02:04.710 --> 02:10.980
default() methods are something which got introduced as part of Java 1.8 . I'll be covering this topic as

31
02:10.980 --> 02:12.900
a separate section in this course.

32
02:12.930 --> 02:15.280
Now let's not worry about the default keyword.

33
02:15.300 --> 02:19.530
Let's go ahead and implement this Comparator interface.

34
02:19.540 --> 02:26.480
Lets  write the code in implementing the Comparator interface the prior Java 8 way and then the lambda approach.

35
02:26.630 --> 02:29.860
Let me give some comment here prior Java 8.

36
02:30.250 --> 02:33.830
And then give Comparator.

37
02:34.200 --> 02:38.200
I'm going to compare two Integers part of this Comparator.

38
02:38.690 --> 02:45.900
Let me give a name and then the prior java is by creating the anonymous classes.

39
02:45.980 --> 02:52.540
Create Comparator instance. There you go !  The Comparator's compare method is here. Now we will write the logic

40
02:52.540 --> 02:53.220
in here.

41
02:53.300 --> 03:00.470
What I am going to do is I'm going to compare I am going to use the compareTo()  method which 

42
03:00.470 --> 03:01.620
is part of the Integer.

43
03:02.060 --> 03:06.290
Lets write the logic in here.

44
03:06.360 --> 03:16.580
If both the values are the same then it is going to return zero which is o1 == o2

45
03:16.580 --> 03:22.770
o1 == o2;

46
03:22.780 --> 03:37.610
Next step is if o1 > o2 then it is going to return 1. Next is -1

47
03:37.710 --> 03:44.110
If o1 < o2. This is what this compareTo()

48
03:44.110 --> 03:46.020
is going to do.

49
03:46.090 --> 03:53.920
Now let's call this anonymous class  implementation. How do we do it ? Am gonna have 

50
03:54.480 --> 03:57.710
Result of the comparator is

51
04:02.460 --> 04:05.510
here we are going to print the result.

52
04:05.660 --> 04:12.530
I'm going to call  comparator and it is going to give the recommendation of compare(). What is the input that

53
04:12.530 --> 04:16.770
we're going to pass ? I'm going to pass (3,2). Lets run this!

54
04:20.080 --> 04:22.020
So the result of the comparator is 1.

55
04:22.020 --> 04:25.510
Until now there is nothing new here.

56
04:25.580 --> 04:32.440
It's just the old way.  Most of the developers who have worked using Comparator they would have

57
04:32.450 --> 04:44.510
Implemented it this way. Now lets implement it using the Lambda way. Comparator <Integer> and

58
04:44.630 --> 04:49.570
am going to give it a name called comparatorLambda = ()

59
04:49.630 --> 04:59.600
If you take a look at the Comparator interface it accepts two inputs, I am going to pass those (Integer a, Integer b)

60
05:00.390 --> 05:05.970
(Integer a, Integer b) -> followed by that lets put in the arrow. The syntax for

61
05:06.040 --> 05:09.260
the lambda body is by giving the curly braces.

62
05:09.300 --> 05:18.040
Right. Now what we will do we will do a.compareTo(b)

63
05:21.430 --> 05:25.730
But there is some  compilation issue! I think the result is have to return this right ? 

64
05:30.680 --> 05:34.530
Lets take a look at the issues that we're facing here is expecting this ";" correct.

65
05:37.800 --> 05:40.010
Ideally if you take a look at it

66
05:40.370 --> 05:43.020
it is just single statement right ?

67
05:43.140 --> 05:45.990
If you remember from the previous tutorial. If it is a single statement

68
05:46.020 --> 05:47.450
we don't even need this.

69
05:52.010 --> 05:52.580
Thats it!

70
05:52.790 --> 06:02.650
So now we have implemented comparator interface using Lambda. Am gonna call this one by copying the same.

71
06:02.660 --> 06:14.860
"Result of the Comparator using Lambda is :" In here we are going to use the comparator. Lets run this.

72
06:17.520 --> 06:20.470
So we have successfully implemented it. The results

73
06:20.540 --> 06:26.420
if you  take a look at it both are the same and we have successfully implemented the Comparator interface

74
06:26.450 --> 06:27.740
using lambda.

75
06:27.760 --> 06:32.430
Next up is here we are explicitly mentioning the type.

76
06:32.450 --> 06:36.880
Ideally you don't have to do it because here you have already mentioned that type.

77
06:37.040 --> 06:41.830
that this particular lambda expression or this Comparator interface is going accept.

78
06:41.900 --> 06:47.110
So in those kind of scenarios we don't even have to provide the type explicitly there.

79
06:47.120 --> 06:55.890
I'm going to name this one as comparatorLambda1. Lambda is really really smart enough to know to

80
06:56.270 --> 07:01.040
type cast this input to integer.

81
07:01.070 --> 07:06.460
So, I'm gonna run this.

82
07:06.690 --> 07:12.760
And then I am going to use the comparatorlambda1 instance and  run this.

83
07:15.540 --> 07:23.300
Result is 1. As part of the tutorial we have explored the lambda expression that accepts

84
07:23.390 --> 07:26.270
an input and returns an output.

85
07:26.560 --> 07:34.430
Advantages are clearly evident if you compared this syntax with this syntax. Lambda lets you write more concise

86
07:34.520 --> 07:36.080
and clear code.

87
07:36.140 --> 07:39.800
It might be difficult to adapt in the beginning, 

88
07:39.800 --> 07:47.820
once you get the hang of the lambda expression. You will love it! I will cover more about Lambdas and its advantages in 

89
07:47.950 --> 07:49.760
the upcoming tutorials.

90
07:49.840 --> 07:51.910
With this we came to the end of the territorial.

91
07:52.070 --> 07:52.940
Thank you for watching.