8- The Consumer Interface
Jun 29, 2026 19:16
· 4:06
· English
· Whisper Turbo
· 4 Đầu ghi
Bản ghi chép này hết hạn hôm nay.
Tăng cấp cho lưu trữ vĩnh viễn →
Chỉ hiển thị
0:03
S…
Speaker 1 (8- The Consumer Interface)
The consumer interface represents an operation that takes a single argument
0:07
S…
Speaker 1 (8- The Consumer Interface)
and doesn't return a value.
0:09
S…
Speaker 4 (8- The Consumer Interface)
Now,
0:10
S…
Speaker 2 (8- The Consumer Interface)
why does this matter?
0:11
S…
Speaker 1 (8- The Consumer Interface)
Why do you have to learn about the consumer interface?
0:14
S…
Speaker 1 (8- The Consumer Interface)
Well, a lot of methods in Java expect an object that implements
0:18
S…
Speaker 1 (8- The Consumer Interface)
the consumer interface.
0:19
S…
Speaker 1 (8- The Consumer Interface)
I will show you an example of this later in this video.
0:22
S…
Speaker 2 (8- The Consumer Interface)
But first,
0:22
S…
Speaker 1 (8- The Consumer Interface)
let's look at the methods of this interface.
0:24
S…
Speaker 2 (8- The Consumer Interface)
So as you can see
0:27
S…
Speaker 1 (8- The Consumer Interface)
Here we have a single abstract method called except that
0:32
S…
Speaker 1 (8- The Consumer Interface)
takes an argument of type T and here T is our generic type parameter and
0:36
S…
Speaker 1 (8- The Consumer Interface)
this method doesn't return a value.
0:38
S…
Speaker 1 (8- The Consumer Interface)
We also have a default method as I told you before these default
0:42
S…
Speaker 1 (8- The Consumer Interface)
methods have an implementation.
0:44
S…
Speaker 1 (8- The Consumer Interface)
I'm going to talk about this method in the next video.
0:47
S…
Speaker 1 (8- The Consumer Interface)
So the consumer interface represents an operation that takes a single
0:51
S…
Speaker 1 (8- The Consumer Interface)
argument and doesn't return a value.
0:53
S…
Speaker 1 (8- The Consumer Interface)
Now we have a few variations of this interface.
0:56
S…
Speaker 1 (8- The Consumer Interface)
One of them is by consumer that takes two arguments.
1:00
S…
Speaker 1 (8- The Consumer Interface)
So if you look at the except method,
1:02
S…
Speaker 2 (8- The Consumer Interface)
you can see here we have two arguments t and u.
1:06
S…
Speaker 2 (8- The Consumer Interface)
We also have int consumer.
1:09
S…
Speaker 1 (8- The Consumer Interface)
This is one of the primitive specializations of this interface.
1:12
S…
Speaker 4 (8- The Consumer Interface)
So
1:13
S…
Speaker 1 (8- The Consumer Interface)
the accept method takes a primitive integer.
1:16
S…
Speaker 1 (8- The Consumer Interface)
So if you're dealing with a lot of integer values,
1:19
S…
Speaker 1 (8- The Consumer Interface)
it is more efficient to use this interface as opposed to the regular consumer
1:24
S…
Speaker 1 (8- The Consumer Interface)
interface because with this interface,
1:26
S…
Speaker 1 (8- The Consumer Interface)
you don't pay the cost of auto boxing.
1:28
S…
Speaker 1 (8- The Consumer Interface)
You also have long consumer and double consumer.
1:32
S…
Speaker 1 (8- The Consumer Interface)
Now let me show you how to use the consumer interface.
1:35
S…
Speaker 2 (8- The Consumer Interface)
I'm going to create a list of integers.
1:40
S…
Speaker 1 (8- The Consumer Interface)
And here I'm going to use list of to initialize this list with
1:44
S…
Speaker 1 (8- The Consumer Interface)
a few items.
1:45
S…
Speaker 4 (8- The Consumer Interface)
So one,
1:46
S…
Speaker 3 (8- The Consumer Interface)
two, and three.
1:47
S…
Speaker 1 (8- The Consumer Interface)
Now there are two ways to iterate over this list.
1:50
S…
Speaker 1 (8- The Consumer Interface)
We can use a for each loop.
1:52
S…
Speaker 2 (8- The Consumer Interface)
So for each item in list print each
1:56
S…
Speaker 2 (8- The Consumer Interface)
item. Now there is another way to iterate over
2:00
S…
Speaker 1 (8- The Consumer Interface)
this list.
2:01
S…
Speaker 1 (8- The Consumer Interface)
We can call list .foreach and take
2:05
S…
Speaker 1 (8- The Consumer Interface)
a look.
2:06
S…
Speaker 1 (8- The Consumer Interface)
this method expects a consumer object.
2:08
S…
Speaker 1 (8- The Consumer Interface)
In fact, a consumer of integer.
2:10
S…
Speaker 2 (8- The Consumer Interface)
As you saw,
2:12
S…
Speaker 1 (8- The Consumer Interface)
the consumer interface is a functional interface because it has a single
2:16
S…
Speaker 3 (8- The Consumer Interface)
abstract method.
2:18
S…
Speaker 1 (8- The Consumer Interface)
So we can represent it using a lambda expression.
2:20
S…
Speaker 2 (8- The Consumer Interface)
In this case,
2:21
S…
Speaker 1 (8- The Consumer Interface)
we want to pass a lambda expression that takes an integer and doesn't
2:26
S…
Speaker 1 (8- The Consumer Interface)
return a value.
2:26
S…
Speaker 2 (8- The Consumer Interface)
So let's pass a lambda expression here.
2:29
S…
Speaker 2 (8- The Consumer Interface)
First we add the parameter integer item.
2:33
S…
Speaker 1 (8- The Consumer Interface)
then the lambda operator that goes to a code block.
2:37
S…
Speaker 2 (8- The Consumer Interface)
Now in this case,
2:39
S…
Speaker 1 (8- The Consumer Interface)
we don't really need to specify the type of this parameter because the
2:43
S…
Speaker 1 (8- The Consumer Interface)
Java compiler knows that every item in this list is an instance of
2:47
S…
Speaker 3 (8- The Consumer Interface)
the integer class.
2:48
S…
Speaker 1 (8- The Consumer Interface)
So this type is redundant.
2:50
S…
Speaker 1 (8- The Consumer Interface)
Let's remove it.
2:51
S…
Speaker 1 (8- The Consumer Interface)
Now we have a single parameter so we can get rid of the parentheses.
2:55
S…
Speaker 1 (8- The Consumer Interface)
That is better.
2:56
S…
Speaker 1 (8- The Consumer Interface)
Now in the body of this lambda,
2:59
S…
Speaker 1 (8- The Consumer Interface)
I just want to print this item.
3:01
S…
Speaker 1 (8- The Consumer Interface)
Because we have a single statement.
3:02
S…
Speaker 1 (8- The Consumer Interface)
We don't even need the code block.
3:04
S…
Speaker 2 (8- The Consumer Interface)
So print item So these are two different ways
3:08
S…
Speaker 1 (8- The Consumer Interface)
to iterate over a list what we have on the top is an example
3:12
S…
Speaker 1 (8- The Consumer Interface)
of imperative programming
3:14
S…
Speaker 1 (8- The Consumer Interface)
That basically means implementing logic using instructions.
3:17
S…
Speaker 2 (8- The Consumer Interface)
For example,
3:18
S…
Speaker 1 (8- The Consumer Interface)
whenever we have for loops or if and else statements or switch
3:23
S…
Speaker 1 (8- The Consumer Interface)
on case, we are writing code in imperative style.
3:26
S…
Speaker 1 (8- The Consumer Interface)
So imperative programming is a style of programming or a programming paradigm.
3:30
S…
Speaker 1 (8- The Consumer Interface)
Now we have so many different programming paradigms or styles of programming like
3:35
S…
Speaker 1 (8- The Consumer Interface)
functional, object oriented,
3:36
S…
Speaker 1 (8- The Consumer Interface)
event driven and so on.
3:38
S…
Speaker 1 (8- The Consumer Interface)
What we have on the bottom is an example of declarative programming.
3:44
S…
Speaker 1 (8- The Consumer Interface)
So instead of using instructions to specify how something should be done,
3:48
S…
Speaker 1 (8- The Consumer Interface)
we specify what needs to be done.
3:51
S…
Speaker 2 (8- The Consumer Interface)
So here we don't have a for statement.
3:53
S…
Speaker 1 (8- The Consumer Interface)
We're simply saying for each item in this list,
3:55
S…
Speaker 3 (8- The Consumer Interface)
print it.
3:56
S…
Speaker 1 (8- The Consumer Interface)
Now we're going to talk more about this concept,
3:59
S…
Speaker 1 (8- The Consumer Interface)
so if this is new to you,
4:00
S…
Speaker 3 (8- The Consumer Interface)
don't worry.
4:00
S…
Speaker 1 (8- The Consumer Interface)
Next we're going to talk about chaining consumers.
Bản ghi này được tạo bởi AI (tự động nhận dạng giọng nói). Có thể chứa lỗi — kiểm tra với âm thanh gốc để sử dụng quan trọng. Chính sách AI
Tóm tắt
Nhấn tổng hợp để tạo một tóm tắt AI của bản ghi này.
Tóm tắt...
Hỏi AI về bản ghi này
Hỏi bất cứ điều gì về bản ghi chép này - AI sẽ tìm thấy các phần liên quan và trả lời.