9- Chaining Consumer
Jun 29, 2026 19:22
· 4:28
· English
· Whisper Turbo
· 2 发言者
本记录稿今天到期。
永久储存的升级 →
仅显示
0:03
S…
Speaker 1 (9- Chaining Consumer)
Earlier I briefly mentioned that the consumer interface has a default method called
0:07
S…
Speaker 1 (9- Chaining Consumer)
and then.
0:09
S…
Speaker 1 (9- Chaining Consumer)
With this method we can chain consumers which basically means we
0:13
S…
Speaker 1 (9- Chaining Consumer)
can run many operations in sequence.
0:15
S…
Speaker 1 (9- Chaining Consumer)
Let me show you how cool and powerful this is.
0:18
S…
Speaker 1 (9- Chaining Consumer)
So I'm going to create a list of strings and
0:22
S…
Speaker 1 (9- Chaining Consumer)
initialize it using list of.
0:26
S…
Speaker 1 (9- Chaining Consumer)
Here I'm going to pass three strings like a,
0:29
S…
Speaker 1 (9- Chaining Consumer)
b,
0:30
S…
Speaker 1 (9- Chaining Consumer)
and c.
0:33
S…
Speaker 1 (9- Chaining Consumer)
Now you learn that the consumer interface represents an operation that takes
0:37
S…
Speaker 1 (9- Chaining Consumer)
a single argument and doesn't return a value.
0:39
S…
Speaker 1 (9- Chaining Consumer)
So let's declare a variable of type consumer of
0:44
S…
Speaker 1 (9- Chaining Consumer)
string.
0:44
S…
Speaker 1 (9- Chaining Consumer)
I'm going to call this print.
0:48
S…
Speaker 1 (9- Chaining Consumer)
Now we should set this to a lambda expression.
0:50
S…
Speaker 1 (9- Chaining Consumer)
This lambda expression should match this method.
0:53
S…
Speaker 1 (9- Chaining Consumer)
So it should have a parameter of type string and it shouldn't
0:57
S…
Speaker 1 (9- Chaining Consumer)
return a value.
0:58
S…
Speaker 1 (9- Chaining Consumer)
So here we add string item goes
1:03
S…
Speaker 1 (9- Chaining Consumer)
to
1:04
S…
Speaker 1 (9- Chaining Consumer)
Let's just print this item.
1:06
S…
Speaker 1 (9- Chaining Consumer)
Now once again,
1:07
S…
Speaker 1 (9- Chaining Consumer)
we can simplify this lambda expression by removing this type because
1:12
S…
Speaker 1 (9- Chaining Consumer)
the Java compiler knows that here we're consuming a string.
1:15
S…
Speaker 1 (9- Chaining Consumer)
So the item that we pass here is a string object.
1:19
S…
Speaker 1 (9- Chaining Consumer)
So there's no need to explicitly specify the type.
1:22
S…
Speaker 1 (9- Chaining Consumer)
Now because we have a single parameter,
1:24
S…
Speaker 1 (9- Chaining Consumer)
we can get rid of the parentheses.
1:25
S…
Speaker 1 (9- Chaining Consumer)
That is better.
1:27
S…
Speaker 1 (9- Chaining Consumer)
So here we're declaring an operation called
1:31
S…
Speaker 1 (9- Chaining Consumer)
print that takes an item and prints it on a terminal.
1:35
S…
Speaker 1 (9- Chaining Consumer)
Now let's declare another operation that prints a string in uppercase.
1:39
S…
Speaker 2 (9- Chaining Consumer)
So,
1:40
S…
Speaker 1 (9- Chaining Consumer)
once again,
1:41
S…
Speaker 1 (9- Chaining Consumer)
consumer of string,
1:43
S…
Speaker 1 (9- Chaining Consumer)
we call it print uppercase.
1:46
S…
Speaker 1 (9- Chaining Consumer)
Pay attention to how I'm naming these consumers.
1:49
S…
Speaker 1 (9- Chaining Consumer)
The name of this variable clearly represents this operation.
1:52
S…
Speaker 2 (9- Chaining Consumer)
So,
1:54
S…
Speaker 1 (9- Chaining Consumer)
once again,
1:55
S…
Speaker 1 (9- Chaining Consumer)
item goes to,
1:56
S…
Speaker 1 (9- Chaining Consumer)
you want to print this,
1:57
S…
Speaker 1 (9- Chaining Consumer)
but first we want to convert it to uppercase.
2:00
S…
Speaker 1 (9- Chaining Consumer)
Pretty straightforward,
2:01
S…
Speaker 1 (9- Chaining Consumer)
right?
2:01
S…
Speaker 1 (9- Chaining Consumer)
Now,
2:02
S…
Speaker 1 (9- Chaining Consumer)
we want to iterate over this list,
2:04
S…
Speaker 1 (9- Chaining Consumer)
and for each item,
2:05
S…
Speaker 1 (9- Chaining Consumer)
we want to execute these two operations in sequence.
2:07
S…
Speaker 1 (9- Chaining Consumer)
So we call list .foreach.
2:10
S…
Speaker 1 (9- Chaining Consumer)
Here we should pass a consumer object,
2:13
S…
Speaker 1 (9- Chaining Consumer)
so I'm going to pass print,
2:15
S…
Speaker 1 (9- Chaining Consumer)
now this consumer has a method called and then.
2:20
S…
Speaker 1 (9- Chaining Consumer)
Now look at the parameter of this method.
2:22
S…
Speaker 1 (9- Chaining Consumer)
Here we can pass a second consumer.
2:25
S…
Speaker 1 (9- Chaining Consumer)
So we can pass print uppercase.
2:28
S…
Speaker 1 (9- Chaining Consumer)
Now, when I run this program,
2:30
S…
Speaker 1 (9- Chaining Consumer)
you'll see that for each item,
2:31
S…
Speaker 1 (9- Chaining Consumer)
first we're going to print that item and then we're going to print it in uppercase.
2:35
S…
Speaker 1 (9- Chaining Consumer)
Take a look.
2:35
S…
Speaker 2 (9- Chaining Consumer)
There you go.
2:39
S…
Speaker 1 (9- Chaining Consumer)
That's pretty cool.
2:40
S…
Speaker 2 (9- Chaining Consumer)
Well,
2:41
S…
Speaker 1 (9- Chaining Consumer)
let me explain how the magic happens under the hood.
2:44
S…
Speaker 1 (9- Chaining Consumer)
So I'm going to put the caret on the method name and then,
2:47
S…
Speaker 1 (9- Chaining Consumer)
now on the top,
2:48
S…
Speaker 1 (9- Chaining Consumer)
if you go to the navigate menu,
2:50
S…
Speaker 1 (9- Chaining Consumer)
look at the shortcut for declaration.
2:54
S…
Speaker 1 (9- Chaining Consumer)
On Mac,
2:55
S…
Speaker 1 (9- Chaining Consumer)
it's command and B.
2:56
S…
Speaker 1 (9- Chaining Consumer)
On Windows,
2:57
S…
Speaker 1 (9- Chaining Consumer)
it's probably control and B.
2:58
S…
Speaker 1 (9- Chaining Consumer)
So I'm going to press command and B.
3:01
S…
Speaker 1 (9- Chaining Consumer)
Now you can see the source code of this method.
3:03
S…
Speaker 1 (9- Chaining Consumer)
So let's see what's happening here.
3:05
S…
Speaker 1 (9- Chaining Consumer)
This method takes a consumer object called after.
3:09
S…
Speaker 1 (9- Chaining Consumer)
First,
3:11
S…
Speaker 1 (9- Chaining Consumer)
it ensures that this argument is not null.
3:13
S…
Speaker 1 (9- Chaining Consumer)
Pretty straightforward.
3:14
S…
Speaker 1 (9- Chaining Consumer)
Now,
3:15
S…
Speaker 1 (9- Chaining Consumer)
look at what is returned here.
3:16
S…
Speaker 1 (9- Chaining Consumer)
We're returning a lambda expression.
3:19
S…
Speaker 1 (9- Chaining Consumer)
This lambda expression represents a consumer object.
3:23
S…
Speaker 1 (9- Chaining Consumer)
So it takes an argument of type T
3:27
S…
Speaker 1 (9- Chaining Consumer)
And in the body of this lambda,
3:28
S…
Speaker 1 (9- Chaining Consumer)
first we call the accept method of the current consumer.
3:32
S…
Speaker 1 (9- Chaining Consumer)
So we're executing the current operation and then we're
3:36
S…
Speaker 1 (9- Chaining Consumer)
calling the accept method of the second consumer.
3:40
S…
Speaker 1 (9- Chaining Consumer)
This is how two operations get executed sequentially.
3:43
S…
Speaker 1 (9- Chaining Consumer)
So every time we call this method,
3:46
S…
Speaker 1 (9- Chaining Consumer)
we get a new consumer.
3:47
S…
Speaker 1 (9- Chaining Consumer)
And that means once again we can call and then on that new consumer
3:51
S…
Speaker 1 (9- Chaining Consumer)
to chain another task.
3:53
S…
Speaker 1 (9- Chaining Consumer)
Let me show you.
3:54
S…
Speaker 2 (9- Chaining Consumer)
So
3:55
S…
Speaker 1 (9- Chaining Consumer)
Back to our demo once we call and then we get
3:59
S…
Speaker 1 (9- Chaining Consumer)
a new consumer object So here we can call and then and then
4:03
S…
Speaker 1 (9- Chaining Consumer)
pass another consumer object like print So with
4:08
S…
Speaker 1 (9- Chaining Consumer)
this code we're executing three operations in sequence
4:12
S…
Speaker 1 (9- Chaining Consumer)
Look at the result.
4:14
S…
Speaker 2 (9- Chaining Consumer)
There you go.
4:18
S…
Speaker 1 (9- Chaining Consumer)
We're printing each item three times on the terminal So
4:23
S…
Speaker 1 (9- Chaining Consumer)
this is how we can chain consumers
这份录音记录是AI(自动语音识别)生成的,可能包含错误——与用于关键用途的原始音频核对。 大赦国际的政策
摘要
点击摘要以生成本记录誊本的 AI 摘要 。
总结中...
询问 AI 有关此分页
询问任何有关这一记录,大赦国际将找到有关章节和答复。