1
00:00:06,140 --> 00:00:06,520
Hello.

2
00:00:06,560 --> 00:00:11,150
Yes, students in this class and we are going to learn what lambda expressions and message references

3
00:00:11,150 --> 00:00:12,030
are today.

4
00:00:12,050 --> 00:00:15,400
We are going to have really a lot of practice after this lesson.

5
00:00:15,410 --> 00:00:18,310
You will be able to write lambda functions like a pro.

6
00:00:18,770 --> 00:00:24,080
I will explain to you what functional interfaces are and we will learn the difference between regular

7
00:00:24,080 --> 00:00:29,600
and functional interfaces by the end of the class and you will know how to create your own functional

8
00:00:29,600 --> 00:00:30,410
interfaces.

9
00:00:30,890 --> 00:00:34,600
We are going to learn what the function is and what Massett reference is.

10
00:00:34,610 --> 00:00:38,710
I prepared special examples that will help you to grasp the knowledge faster.

11
00:00:39,110 --> 00:00:44,160
I'm going to show you how you can write lambda functions and how you can use math references.

12
00:00:44,660 --> 00:00:45,380
Let's start.

13
00:00:46,010 --> 00:00:49,940
And today, the best way to learn the topic is start from examples.

14
00:00:50,420 --> 00:00:54,050
That's why I suggest switching to the eclipse and start our lesson.

15
00:00:54,620 --> 00:00:59,690
To understand how to use Lambda functions and Macit references, we have to understand first what functional

16
00:00:59,690 --> 00:01:00,590
interface is.

17
00:01:01,040 --> 00:01:06,200
As we briefly discussed in previous lesson, functional interface is an interface that has a single

18
00:01:06,200 --> 00:01:08,180
object method declared in it.

19
00:01:09,000 --> 00:01:13,060
Take a look at this example, this interface is called Distance Calculator.

20
00:01:13,380 --> 00:01:19,650
It is declared to have Iraqi of types that can calculate distance of delivery of customer's order in

21
00:01:19,650 --> 00:01:20,100
miles.

22
00:01:21,180 --> 00:01:27,840
I can calculate the shortest path, the most optimal road route for interstate delivery, distance from

23
00:01:27,840 --> 00:01:31,290
the closest store or from the specific store and lots more.

24
00:01:31,800 --> 00:01:38,790
All this might be implemented in calculate distance method in other types that will implement the Easton's

25
00:01:38,790 --> 00:01:39,440
calculator.

26
00:01:40,050 --> 00:01:43,050
This method also takes two objects of city type.

27
00:01:43,260 --> 00:01:45,240
The store inside coordinates.

28
00:01:45,840 --> 00:01:47,690
We have city time declared here.

29
00:01:47,880 --> 00:01:50,400
This is a type created just for our example.

30
00:01:50,730 --> 00:01:56,520
The state of the city can be described with the help of the coordinates, longitude and latitude, and

31
00:01:56,520 --> 00:01:58,020
we have getters and setters here.

32
00:01:58,740 --> 00:02:01,980
Now you have the context of real life business case.

33
00:02:02,220 --> 00:02:07,380
I believe that understanding of real life example will help you to grasp new knowledge faster.

34
00:02:08,100 --> 00:02:08,970
That is great.

35
00:02:09,480 --> 00:02:13,110
You can see new annotations here, functional interface.

36
00:02:13,410 --> 00:02:14,270
What is it for?

37
00:02:14,790 --> 00:02:20,670
First of all, this is a great sign to notify clients of your code that originally regionalisms interface

38
00:02:20,670 --> 00:02:24,810
is created to be a target type for lambda functions and Macit references.

39
00:02:25,410 --> 00:02:30,990
And the second reason to use the sanitation is that in case somebody would try to add additional abasic

40
00:02:30,990 --> 00:02:33,500
Macit, there will be a compilation error.

41
00:02:33,990 --> 00:02:38,270
Let me comment this line of code that contains declaration of additional Massett.

42
00:02:38,760 --> 00:02:41,730
And here you can see we have a compilation error now.

43
00:02:42,190 --> 00:02:48,180
That's why functional interface annotation helps us to prevent cases when someone wants to add additional

44
00:02:48,180 --> 00:02:53,990
behavior to the type that we already used in other parts of the program as a tag for lambda function.

45
00:02:54,630 --> 00:02:57,280
Hope it is clear what function interface is.

46
00:02:57,780 --> 00:03:00,270
Let me comment this MassArt again to proceed.

47
00:03:00,960 --> 00:03:06,360
Pay attention to the fact that functional interface can contain as much default methods as possible

48
00:03:06,360 --> 00:03:08,580
and as much static methods as possible.

49
00:03:08,760 --> 00:03:12,030
This does not violate the rule for functional interface.

50
00:03:12,450 --> 00:03:16,280
That said, regarding functional interface, that was easy and great.

51
00:03:16,860 --> 00:03:18,200
Let me congratulate you.

52
00:03:18,390 --> 00:03:22,050
Now you know how to create your own custom functional interface.

53
00:03:22,710 --> 00:03:27,750
And now when you know what functional interface is, let's learn what lambda expressions are.

54
00:03:28,230 --> 00:03:31,530
Let me open the next examples that I have prepared before this lesson.

55
00:03:32,160 --> 00:03:37,800
I have a few more classes grouped here in one file, just for the sake of example, to not switch tabs

56
00:03:37,800 --> 00:03:39,000
between different files.

57
00:03:39,570 --> 00:03:45,810
Imagine that we have class with a name or the management, and this class requires an instance of distance

58
00:03:45,810 --> 00:03:52,410
calculator type to perform all operations related to the management we can pass in terms of distance

59
00:03:52,410 --> 00:03:57,230
calculator type, either with the help of constructor or with the help of the setup here.

60
00:03:57,810 --> 00:04:03,420
That means you need to create an object of type distance calculator in your program, but you know that

61
00:04:03,420 --> 00:04:06,470
you can't create objects on the basis of interface, right?

62
00:04:06,930 --> 00:04:12,870
That's why you declare concrete class, let's say the full distance calculator and implement distance

63
00:04:12,870 --> 00:04:14,430
calculator interface here.

64
00:04:14,970 --> 00:04:20,910
Now when we create object of type order management, we great object of type distance calculator.

65
00:04:21,360 --> 00:04:27,900
But what to do in case we need to change this since calculator to take into account specifics of inter-state

66
00:04:27,900 --> 00:04:28,560
delivery.

67
00:04:28,950 --> 00:04:35,670
In this case, we have to create another concrete class and we need this just override, calculate distance

68
00:04:35,670 --> 00:04:36,120
method.

69
00:04:36,120 --> 00:04:38,310
Only once is it worth it.

70
00:04:38,820 --> 00:04:44,970
We've seen that this doesn't work of our efforts and we decide to declare anonymous class and create

71
00:04:44,970 --> 00:04:46,680
an instance of anonymous class.

72
00:04:47,160 --> 00:04:51,960
This time we take into account logic for interstate distance calculation.

73
00:04:52,500 --> 00:04:55,980
OK, but can you see how much lines of code it takes?

74
00:04:56,220 --> 00:05:02,250
It is super weird that we have to declare a concrete class just to give implementation of one single

75
00:05:02,250 --> 00:05:02,700
method.

76
00:05:03,330 --> 00:05:07,500
We can create thousands of concrete classes to implement dozens of functions.

77
00:05:08,130 --> 00:05:14,190
But do we need to overwhelm our application with additional classes if we just need one function in

78
00:05:14,190 --> 00:05:15,360
one single place?

79
00:05:16,020 --> 00:05:19,620
No, that's where lambda function is going to help us.

80
00:05:20,160 --> 00:05:22,080
But what is lambda function?

81
00:05:22,500 --> 00:05:24,750
It is also called anonymous function.

82
00:05:24,990 --> 00:05:26,460
It doesn't have any name.

83
00:05:27,030 --> 00:05:30,960
Usually anonymous functions are passed to higher order functions.

84
00:05:31,470 --> 00:05:38,430
Also, in case the function is only used once a limited number of times, it will be easier to Islam

85
00:05:38,430 --> 00:05:44,220
the expression rather than the clear and concrete class in need to create an object of time distance

86
00:05:44,220 --> 00:05:44,820
calculator.

87
00:05:44,830 --> 00:05:51,060
We have to just implement one method, take a look here and declare a new variable of distance calculator

88
00:05:51,060 --> 00:05:54,570
type to describe new logic for calculate distance method.

89
00:05:54,960 --> 00:05:56,850
And here you can see the land expression.

90
00:05:57,210 --> 00:06:00,900
Here it is lambda function, anonymous function.

91
00:06:01,140 --> 00:06:04,560
We can call this in different ways by using different names.

92
00:06:05,170 --> 00:06:07,500
Let me explain in the syntax here.

93
00:06:07,500 --> 00:06:07,920
You can see.

94
00:06:07,980 --> 00:06:13,850
Parentheses that we usually use for MassArt invocation, Autobus arguments, you can see that I Passey

95
00:06:13,850 --> 00:06:19,010
Q1 and Q2 arguments where I this I didn't create any objects.

96
00:06:19,310 --> 00:06:24,800
This is just local MASAD variables similar to the ones we use in massive declaration.

97
00:06:25,430 --> 00:06:31,730
Using these variables I can get access to objects at my function should work with inside the body.

98
00:06:32,150 --> 00:06:33,710
I will show you that in a second.

99
00:06:34,250 --> 00:06:40,490
Why I don't write types of these local variables because compiler knows that I want to give implementation

100
00:06:40,490 --> 00:06:44,870
of the method from distance calculator type and the first argument.

101
00:06:44,870 --> 00:06:48,590
And this method has typesetter the same as the second argument.

102
00:06:49,070 --> 00:06:53,320
That's why Compiler knows that these local variables should be of typesetter.

103
00:06:53,870 --> 00:06:58,820
But in case I will add types here, there is no harm in this, just like this.

104
00:06:59,830 --> 00:07:05,170
In case if Jason's calculator wouldn't declare any Masset parameters and calculate distance measured

105
00:07:05,380 --> 00:07:11,620
in that case, we would skip Masad arguments here and would leave just blank parentheses like this.

106
00:07:15,760 --> 00:07:18,220
Here is LAMDA and here goes returned value.

107
00:07:19,060 --> 00:07:20,890
Here is some damage calculation.

108
00:07:20,920 --> 00:07:25,090
For example, I perform subtraction of longitude of 31 and 32.

109
00:07:25,510 --> 00:07:30,310
That doesn't give us a true distance between cities, but makes you understand that you can perform

110
00:07:30,310 --> 00:07:31,840
any calculations here.

111
00:07:32,500 --> 00:07:38,950
And the result of this calculation is returns from the MassArt worse given return keyword here, because

112
00:07:38,950 --> 00:07:44,140
Compellent knows that at one point of implementation of the absolute masset of distance calculate the

113
00:07:44,150 --> 00:07:47,710
time and compiler knows that this method should return value.

114
00:07:48,340 --> 00:07:50,940
Technically creation of object of anonymous glass.

115
00:07:50,950 --> 00:07:56,320
And this is the same because calculate distance implementation is the same as here.

116
00:07:57,360 --> 00:08:03,660
The next point you have to know about our Syntex in case our anonymous function consists of more than

117
00:08:03,660 --> 00:08:06,360
one line, then we need to use Brace's.

118
00:08:06,780 --> 00:08:08,310
And here you can see an example.

119
00:08:08,970 --> 00:08:12,330
Also, in this case, we have to right return keywords.

120
00:08:13,020 --> 00:08:18,600
And if you see that in this case, visually, this LAMDA function is closer to the method we have here

121
00:08:18,600 --> 00:08:25,140
in anonymous class where missing only a return type value method, name type of local variables and

122
00:08:25,140 --> 00:08:26,080
access modifier.

123
00:08:26,670 --> 00:08:33,030
So in case I write here, public double calculate distance and in case I would add types to local variables,

124
00:08:33,240 --> 00:08:35,130
does it make more sense like this?

125
00:08:36,900 --> 00:08:42,930
But for lambda expression, we don't need to specify access modifier, it is already accessed here,

126
00:08:43,380 --> 00:08:45,470
let me remove it also.

127
00:08:45,480 --> 00:08:52,320
We don't have to specify return that because our Java compiler can understand that we are going to implement

128
00:08:52,320 --> 00:08:57,650
Macit from a distance, calculate the functional interface and the method inside this interface has

129
00:08:57,660 --> 00:08:58,840
double return type.

130
00:08:59,490 --> 00:09:05,160
Can you understand now why it is important that functional interface has only one abstract method?

131
00:09:05,820 --> 00:09:11,460
Because in case functional interface has more than one method, then it is not clear what message I

132
00:09:11,460 --> 00:09:14,380
try to implement with the help of anonymous function.

133
00:09:14,760 --> 00:09:15,650
Is that clear?

134
00:09:16,380 --> 00:09:23,400
So I can remove, recontact and also I can remove massive name because it is clear what method I want

135
00:09:23,400 --> 00:09:28,170
to implement and I don't need any other identifier to access this function.

136
00:09:28,650 --> 00:09:34,710
And I believe you already understood why I can remove all types of my local variables, because compiler

137
00:09:34,710 --> 00:09:41,280
knows that these variables will be of type according to the interface here you can see that I can call

138
00:09:41,280 --> 00:09:46,650
calculate distance method on my reference here and I believe you already understood that these lines

139
00:09:46,650 --> 00:09:48,210
of code will be executed.

140
00:09:48,450 --> 00:09:49,500
Let me prove this.

141
00:09:49,830 --> 00:09:54,660
I run the program and here you can see console output from the lambda function.

142
00:09:55,080 --> 00:10:01,530
And when I need implementation of distance calculator interface and I need it only one time only it

143
00:10:01,530 --> 00:10:07,830
is easier for me to create lambda function rather than declare a concrete class and create an instance

144
00:10:07,830 --> 00:10:08,910
of a concrete class.

145
00:10:09,360 --> 00:10:16,050
For example, here in the set method, I can pass any function I need and that will be enough for order

146
00:10:16,050 --> 00:10:22,740
management because all the management is interested not in the fact of get an object of type discount

147
00:10:22,740 --> 00:10:23,580
calculator.

148
00:10:24,030 --> 00:10:31,260
Order management is interested in getting behavior to calculate distance and that gives a specific definition

149
00:10:31,260 --> 00:10:34,080
of behavior with the help of lambda function.

150
00:10:34,770 --> 00:10:41,010
Usually it is not instantly clear for my students where we will get arguments for the lambda function.

151
00:10:41,640 --> 00:10:44,310
I want you to understand that lambda function.

152
00:10:44,310 --> 00:10:47,280
It is not a method invocation with arguments.

153
00:10:47,820 --> 00:10:54,150
It is just the definition of the method with parameters similar to this thing we do when we implement

154
00:10:54,150 --> 00:10:56,220
any interface in concrete class.

155
00:10:56,880 --> 00:11:03,210
So the city objects will be passed here to this function, wherever it will be needed.

156
00:11:03,210 --> 00:11:06,480
According to the logic described in the order management.

157
00:11:07,080 --> 00:11:14,160
Sometimes we only know that some type like order management just needs logic to calculate distance between

158
00:11:14,160 --> 00:11:14,910
two cities.

159
00:11:15,270 --> 00:11:20,280
And sometimes we really don't care where order management will get those cities.

160
00:11:20,640 --> 00:11:26,250
Probably they will be passed to other methods of order management or will be present in all the details.

161
00:11:26,550 --> 00:11:32,580
But we let all the management object decide when to call our function and we let all the management

162
00:11:32,580 --> 00:11:35,040
bother about what just to pass there.

163
00:11:35,710 --> 00:11:36,610
Does it make sense?

164
00:11:37,230 --> 00:11:39,420
Hope you understood what lambda function is.

165
00:11:39,750 --> 00:11:42,510
Now, let me explain to you what a reference is.

166
00:11:42,990 --> 00:11:44,730
Imagine that you already have mass.

167
00:11:44,860 --> 00:11:47,160
That's exactly the same what you want.

168
00:11:47,610 --> 00:11:52,740
Probably it is from some external library or probably it is just from another class.

169
00:11:53,160 --> 00:11:58,580
Then why do we need to declare a concrete class to override MassArt or create lambda function?

170
00:11:59,130 --> 00:12:04,890
Why don't we just tell the order management object to use the mass that already exists?

171
00:12:05,190 --> 00:12:10,320
In all cases, when all the management needs to calculate distance, we can do so.

172
00:12:10,830 --> 00:12:13,320
And for this we have to use MassArt references.

173
00:12:13,570 --> 00:12:15,270
Let me show you the next example.

174
00:12:15,960 --> 00:12:18,450
Here is the example of some third party class.

175
00:12:18,750 --> 00:12:24,900
Imagine that it is called Google Dist. calculator and it has Masset for distance calculation.

176
00:12:25,320 --> 00:12:27,630
It is called get distance between cities.

177
00:12:27,960 --> 00:12:31,050
You can see that we have static and non static MassArt.

178
00:12:31,530 --> 00:12:34,050
That is, for the sake of example, to cover a few cases.

179
00:12:34,680 --> 00:12:40,710
Please pay attention to the fact that type of return value and type of method parameters is the same

180
00:12:40,710 --> 00:12:43,230
as we have in distance calculator interface.

181
00:12:43,590 --> 00:12:44,670
This is important.

182
00:12:45,150 --> 00:12:51,480
And now when I need to set new distance calculator into my or the management object, I just pass the

183
00:12:51,480 --> 00:12:52,890
reference to this method.

184
00:12:53,380 --> 00:12:54,340
Let me explain this.

185
00:12:54,340 --> 00:13:01,650
This index here here is the name of our class that contains the message that we need, followed by double

186
00:13:01,650 --> 00:13:02,070
column.

187
00:13:02,640 --> 00:13:09,930
This is a syntax of reference I'm referencing to the distance between a static method in Google distance

188
00:13:09,930 --> 00:13:10,980
calculator class.

189
00:13:11,280 --> 00:13:12,150
Does it make sense?

190
00:13:12,540 --> 00:13:15,150
And again, this is not in location.

191
00:13:15,540 --> 00:13:22,470
That's why I don't pass any arguments here, or the management just needs something that can take two

192
00:13:22,470 --> 00:13:24,630
cities and will return double value.

193
00:13:25,080 --> 00:13:31,470
That's all what order management object expects from distance calculator type and this method from Google

194
00:13:31,470 --> 00:13:35,910
Distance Calculator do the exact things that order management wants.

195
00:13:36,550 --> 00:13:43,210
That's why there is no compilation error here, pay attention that I use class name to make a reference

196
00:13:43,210 --> 00:13:47,770
to static method in case I want to make a reference to non-cyclical method.

197
00:13:47,770 --> 00:13:49,210
I have to create object.

198
00:13:49,610 --> 00:13:52,080
And here you can see that I created object.

199
00:13:52,600 --> 00:13:57,490
Now I can use variable of my object to make a reference to the non static method.

200
00:13:57,790 --> 00:13:58,730
Does it make sense?

201
00:13:59,500 --> 00:14:02,430
Hope now it is clear how Massata references work.

202
00:14:03,070 --> 00:14:04,670
They learned a lot today.

203
00:14:04,990 --> 00:14:11,410
Let's recap what we have learned today, the day we learned what functional interface is and we even

204
00:14:11,410 --> 00:14:13,580
created a custom functional interface.

205
00:14:13,990 --> 00:14:18,920
Now you know what the function is and how to write your own lambda expression.

206
00:14:19,540 --> 00:14:22,300
After that, we learned what math reference is.

207
00:14:22,750 --> 00:14:25,770
On the real example, we learned how to apply your knowledge.

208
00:14:26,410 --> 00:14:27,810
That's it for this lesson.

209
00:14:28,420 --> 00:14:29,650
Thank you for your attention.

210
00:14:29,920 --> 00:14:31,630
See you in the next lesson.
