4- Lambda Expressions
Jun 26, 2026 23:13
· 3:43
· English
· Whisper Turbo
· 2 གསལ་བཤད་
ཡིག་ཆ་འདི་ཕྱི་ཚེས་ ༡༥ ཉིན་མཇུག་བསྡུ་ནི་ཨིན་མས།
གནས་བརྟན་གྱི་གནས་བཞག་གོང་འཕེལ་བཏང། →
སྟོན་རྐྱངམ་ཅིག་
0:03
S…
Speaker 2 (4- Lambda Expressions)
So our greet method talks to the printer interface,
0:06
S…
Speaker 2 (4- Lambda Expressions)
and this is a functional interface because it has a single
0:10
S…
Speaker 2 (4- Lambda Expressions)
abstract method.
0:11
S…
Speaker 1 (4- Lambda Expressions)
Now,
0:12
S…
Speaker 2 (4- Lambda Expressions)
wouldn't it be nice if we could implement this method in a standalone function,
0:16
S…
Speaker 2 (4- Lambda Expressions)
like a function that exists on its own without belonging to a class.
0:20
S…
Speaker 2 (4- Lambda Expressions)
That is what lambda expressions are for.
0:22
S…
Speaker 2 (4- Lambda Expressions)
So a lambda expression is like an anonymous function that we can pass around.
0:27
S…
Speaker 1 (4- Lambda Expressions)
Let me show you.
0:27
S…
Speaker 2 (4- Lambda Expressions)
So I'm going to rewrite this code using a lambda expression.
0:31
S…
Speaker 1 (4- Lambda Expressions)
First recall,
0:33
S…
Speaker 2 (4- Lambda Expressions)
the greet method.
0:34
S…
Speaker 2 (4- Lambda Expressions)
Now here we want to pass an implementation of the print method as
0:38
S…
Speaker 2 (4- Lambda Expressions)
an anonymous function.
0:39
S…
Speaker 2 (4- Lambda Expressions)
So this print method has a
0:44
S…
Speaker 2 (4- Lambda Expressions)
single parameter of type string.
0:45
S…
Speaker 2 (4- Lambda Expressions)
Let's add that over here.
0:47
S…
Speaker 2 (4- Lambda Expressions)
This is where we list our parameters.
0:49
S…
Speaker 2 (4- Lambda Expressions)
So string message.
0:51
S…
Speaker 2 (4- Lambda Expressions)
After parenthesis,
0:53
S…
Speaker 2 (4- Lambda Expressions)
we add an arrow.
0:55
S…
Speaker 2 (4- Lambda Expressions)
This is called the lambda operator.
0:57
S…
Speaker 2 (4- Lambda Expressions)
And then we add braces to represent the body of this
1:01
S…
Speaker 1 (4- Lambda Expressions)
function.
1:01
S…
Speaker 1 (4- Lambda Expressions)
Now,
1:03
S…
Speaker 2 (4- Lambda Expressions)
what should we do here?
1:04
S…
Speaker 2 (4- Lambda Expressions)
We should add a print statement and print this message on the
1:08
S…
Speaker 2 (4- Lambda Expressions)
console. What we have over here is called a lambda expression.
1:12
S…
Speaker 2 (4- Lambda Expressions)
So if you have a functional interface,
1:15
S…
Speaker 2 (4- Lambda Expressions)
we can represent this functional interface using a lambda expression.
1:19
S…
Speaker 2 (4- Lambda Expressions)
That is why these interfaces are called functional,
1:22
S…
Speaker 2 (4- Lambda Expressions)
because they represent a function.
1:23
S…
Speaker 2 (4- Lambda Expressions)
So here we have a lambda expression.
1:27
S…
Speaker 2 (4- Lambda Expressions)
As you can see,
1:28
S…
Speaker 2 (4- Lambda Expressions)
this code is very clean and concise,
1:30
S…
Speaker 2 (4- Lambda Expressions)
but we can make it better.
1:31
S…
Speaker 1 (4- Lambda Expressions)
First of all,
1:32
S…
Speaker 2 (4- Lambda Expressions)
we can remove the type of this parameter.
1:35
S…
Speaker 2 (4- Lambda Expressions)
Now, the Java compiler knows that this parameter is a string object
1:40
S…
Speaker 2 (4- Lambda Expressions)
because the lambda expression that we pass over here will be checked against
1:44
S…
Speaker 2 (4- Lambda Expressions)
the signature of the print method.
1:46
S…
Speaker 2 (4- Lambda Expressions)
So the Java compiler knows that this method has a single parameter and
1:50
S…
Speaker 2 (4- Lambda Expressions)
the type of this parameter is string.
1:51
S…
Speaker 2 (4- Lambda Expressions)
So we don't have to repeat that type over here.
1:54
S…
Speaker 2 (4- Lambda Expressions)
If I type message dot,
1:57
S…
Speaker 2 (4- Lambda Expressions)
now you can see all the string methods.
1:59
S…
Speaker 1 (4- Lambda Expressions)
By the same token,
2:01
S…
Speaker 2 (4- Lambda Expressions)
if I add another parameter here,
2:04
S…
Speaker 2 (4- Lambda Expressions)
The Java compiler complains because it knows that our print method has
2:08
S…
Speaker 2 (4- Lambda Expressions)
a single parameter,
2:09
S…
Speaker 2 (4- Lambda Expressions)
but here we're passing two parameters.
2:11
S…
Speaker 2 (4- Lambda Expressions)
Okay, so for the most part,
2:13
S…
Speaker 2 (4- Lambda Expressions)
we can exclude the type of parameters and let the Java compiler infer them.
2:17
S…
Speaker 1 (4- Lambda Expressions)
Now,
2:18
S…
Speaker 2 (4- Lambda Expressions)
if you have a single parameter,
2:19
S…
Speaker 2 (4- Lambda Expressions)
we can also remove this parenthesis.
2:22
S…
Speaker 2 (4- Lambda Expressions)
That makes our code cleaner and more concise.
2:25
S…
Speaker 2 (4- Lambda Expressions)
We use parenthesis only if we have no parameters or
2:29
S…
Speaker 2 (4- Lambda Expressions)
if you have a method with multiple parameters.
2:32
S…
Speaker 2 (4- Lambda Expressions)
So we separate these parameters using a comma,
2:34
S…
Speaker 2 (4- Lambda Expressions)
just like how we declare the parameters of a method,
2:37
S…
Speaker 1 (4- Lambda Expressions)
okay?
2:38
S…
Speaker 2 (4- Lambda Expressions)
So here we have a single parameter,
2:41
S…
Speaker 2 (4- Lambda Expressions)
and we don't need parentheses.
2:42
S…
Speaker 1 (4- Lambda Expressions)
Also,
2:43
S…
Speaker 2 (4- Lambda Expressions)
if the body of this function has a single line of code,
2:46
S…
Speaker 2 (4- Lambda Expressions)
we don't need these braces either.
2:48
S…
Speaker 2 (4- Lambda Expressions)
So I can clean up this code by pressing alt and enter,
2:52
S…
Speaker 2 (4- Lambda Expressions)
and selecting replace with expression lambda.
2:55
S…
Speaker 1 (4- Lambda Expressions)
There you go.
2:58
S…
Speaker 2 (4- Lambda Expressions)
So this lambda expression looks really clean and concise.
3:00
S…
Speaker 2 (4- Lambda Expressions)
Compare this with an anonymous inner class.
3:04
S…
Speaker 1 (4- Lambda Expressions)
This is really ugly.
3:05
S…
Speaker 1 (4- Lambda Expressions)
So let me remove this.
3:07
S…
Speaker 1 (4- Lambda Expressions)
Alright,
3:10
S…
Speaker 2 (4- Lambda Expressions)
now here we're passing a lambda expression as an argument to a method,
3:13
S…
Speaker 2 (4- Lambda Expressions)
but we can also store a lambda expression in a variable.
3:17
S…
Speaker 1 (4- Lambda Expressions)
For example,
3:18
S…
Speaker 2 (4- Lambda Expressions)
we can declare a variable of type printer and set
3:22
S…
Speaker 2 (4- Lambda Expressions)
it to a new console printer.
3:25
S…
Speaker 2 (4- Lambda Expressions)
So this is the concrete implementation or we can set this
3:29
S…
Speaker 2 (4- Lambda Expressions)
to a lambda expression.
3:30
S…
Speaker 2 (4- Lambda Expressions)
So here we say message goes to print message.
3:34
S…
Speaker 2 (4- Lambda Expressions)
So lambda expressions are essentially objects,
3:38
S…
Speaker 2 (4- Lambda Expressions)
but we can use them to represent anonymous functions.
ཡིག་སྒྱུར་འདི་ བཅོས་མའི་བློ་རིག་ (སྒྲ་ངོས་འཛིན་བྱེད་པའི་འཕྲུལ་རིག་) གིས་བཟོ་སྟེ་ཡོདཔ་ཨིན། འཛོལ་བ་འབྱུང་སྲིད་པ་ལ་ — གལ་སྲིད་ཁག་ཆེ་བའི་དོན་ལས་ ངོ་མ་གི་སྒྲ་སྐད་དང་འཕྱད་ཞིབ་འབད་དགོཔ་ཨིན། བཅོས་མའི་བློ་རིག་གི་སྲིད་བྱུས་
བཅུད་དོན་
ཡིག་ཆ་འདི་གི་ AI བཅུད་བསྡུ་ཐོན་ནི་གི་དོན་ལས་ བཅུད་བསྡུ་ ཟེར་བའི་བསྒང་འདི་བསྒང་བསྒ
དྲན་ཐོ་བསྡུ་གསོག...
དྲན་ཤེས་ལ་དྲི་བ་དྲིས་ལན་ཞུ།
ཡིག་ཆ་འདི་གི་སྐོར་ལ་དྲི་བ་ཅི་རིགས་ཞུས་ནའང་ བཅོས་མའི་བློ་རིག་གིས་ འབྲེལ་བ་ཡོད་པའི་དོན་ཚན་ཚུ་ འཚོལ་ཞིབ་ དང་ལན་འདེབས་ འབད་འོང་།