4- The Iterable Interface
Jun 21, 2026 08:49
· 5:24
· English
· Whisper Turbo
· 2 Speaker
Transkrip ini kadaluarsa hari ini.
Tingkatkan untuk penyimpanan permanen →
Tampilkan saja
0:03
S…
Speaker 2 (4- The Iterable Interface)
The iterable interface represents an object that is iterable.
0:06
S…
Speaker 2 (4- The Iterable Interface)
That means we can iterate or loop over it without knowing anything
0:11
S…
Speaker 2 (4- The Iterable Interface)
about its implementation detail.
0:12
S…
Speaker 2 (4- The Iterable Interface)
So we are using an array here.
0:14
S…
Speaker 2 (4- The Iterable Interface)
If we decide to replace this with an array list or a different data structure
0:18
S…
Speaker 1 (4- The Iterable Interface)
in the future,
0:19
S…
Speaker 2 (4- The Iterable Interface)
the clients of this class are not going to get affected because they are going to code
0:23
S…
Speaker 2 (4- The Iterable Interface)
against the iterable interface.
0:25
S…
Speaker 2 (4- The Iterable Interface)
Back to our remote control metaphor,
0:27
S…
Speaker 2 (4- The Iterable Interface)
think of the buttons for changing the volume.
0:29
S…
Speaker 2 (4- The Iterable Interface)
If the internal implementation of this remote control changes,
0:32
S…
Speaker 1 (4- The Iterable Interface)
you're not going to get affected.
0:34
S…
Speaker 2 (4- The Iterable Interface)
You're still going to use the same buttons.
0:36
S…
Speaker 2 (4- The Iterable Interface)
These buttons are the interfaces that you work with.
0:39
S…
Speaker 2 (4- The Iterable Interface)
So we want to implement the iterable interface in this class and then
0:43
S…
Speaker 2 (4- The Iterable Interface)
have other classes that use our generic list program against the iterable interface.
0:48
S…
Speaker 2 (4- The Iterable Interface)
It's like saying that they use the volume control buttons on a remote control.
0:52
S…
Speaker 1 (4- The Iterable Interface)
Okay, now
0:53
S…
Speaker 2 (4- The Iterable Interface)
Here's the documentation for the iterable interface.
0:56
S…
Speaker 1 (4- The Iterable Interface)
As you can see,
0:57
S…
Speaker 2 (4- The Iterable Interface)
this interface declares three methods.
0:59
S…
Speaker 2 (4- The Iterable Interface)
The first and last methods have a default implementation so we don't
1:03
S…
Speaker 1 (4- The Iterable Interface)
have to implement them.
1:04
S…
Speaker 2 (4- The Iterable Interface)
The only method that we have to implement
1:07
S…
Speaker 2 (4- The Iterable Interface)
is this method over here,
1:08
S…
Speaker 2 (4- The Iterable Interface)
iterator.
1:09
S…
Speaker 2 (4- The Iterable Interface)
It returns an iterator object.
1:12
S…
Speaker 2 (4- The Iterable Interface)
We're going to talk about iterators in the next video,
1:14
S…
Speaker 2 (4- The Iterable Interface)
but very briefly an iterator is an object that we use to iterate
1:18
S…
Speaker 2 (4- The Iterable Interface)
over an iterable.
1:20
S…
Speaker 2 (4- The Iterable Interface)
Now I know the name is confusing,
1:21
S…
Speaker 1 (4- The Iterable Interface)
but don't worry,
1:22
S…
Speaker 1 (4- The Iterable Interface)
I'm going to make it super simple for you.
1:24
S…
Speaker 1 (4- The Iterable Interface)
So,
1:24
S…
Speaker 2 (4- The Iterable Interface)
back to our generic list class.
1:27
S…
Speaker 2 (4- The Iterable Interface)
Here we type implements iterable of
1:31
S…
Speaker 1 (4- The Iterable Interface)
t.
1:32
S…
Speaker 2 (4- The Iterable Interface)
So this is also a generic interface.
1:35
S…
Speaker 2 (4- The Iterable Interface)
Now we press alt and enter and have IntelliJ implement
1:40
S…
Speaker 1 (4- The Iterable Interface)
the methods.
1:40
S…
Speaker 2 (4- The Iterable Interface)
So here you can see the same three methods that I showed
1:44
S…
Speaker 2 (4- The Iterable Interface)
you. The iterator method is highlighted because it's a required method.
1:48
S…
Speaker 2 (4- The Iterable Interface)
So let's press ok and here you can see the iterator
1:53
S…
Speaker 1 (4- The Iterable Interface)
method.
1:53
S…
Speaker 2 (4- The Iterable Interface)
For now don't worry about the code we're going to write in this method.
1:56
S…
Speaker 2 (4- The Iterable Interface)
We're going to come back and complete this in the next video.
1:58
S…
Speaker 2 (4- The Iterable Interface)
All I want you to pay attention to is the return type of this method.
2:02
S…
Speaker 2 (4- The Iterable Interface)
It returns an iterator object.
2:05
S…
Speaker 2 (4- The Iterable Interface)
So, back to our main method.
2:07
S…
Speaker 2 (4- The Iterable Interface)
Now that our list implements the iterable interface,
2:11
S…
Speaker 2 (4- The Iterable Interface)
we know that it has the iterator method.
2:14
S…
Speaker 2 (4- The Iterable Interface)
So we call it to get an iterator,
2:17
S…
Speaker 2 (4- The Iterable Interface)
and with this we can iterate or loop over our list
2:21
S…
Speaker 2 (4- The Iterable Interface)
without knowing anything about its internal implementation.
2:24
S…
Speaker 2 (4- The Iterable Interface)
So we can write code like this.
2:26
S…
Speaker 2 (4- The Iterable Interface)
While iterator has next,
2:29
S…
Speaker 2 (4- The Iterable Interface)
we're going to call iterator
2:32
S…
Speaker 1 (4- The Iterable Interface)
dot next.
2:33
S…
Speaker 2 (4- The Iterable Interface)
This will return the current object.
2:35
S…
Speaker 2 (4- The Iterable Interface)
So we store it over here and then print it on
2:39
S…
Speaker 1 (4- The Iterable Interface)
the console.
2:40
S…
Speaker 2 (4- The Iterable Interface)
What's going on here?
2:41
S…
Speaker 2 (4- The Iterable Interface)
Well, imagine our list looks like this.
2:45
S…
Speaker 1 (4- The Iterable Interface)
So we have a,
2:46
S…
Speaker 1 (4- The Iterable Interface)
b, and c.
2:47
S…
Speaker 2 (4- The Iterable Interface)
This iterator is like a pointer that is pointing at one object.
2:51
S…
Speaker 2 (4- The Iterable Interface)
So initially it's pointing at the first object.
2:55
S…
Speaker 2 (4- The Iterable Interface)
At the beginning of our loop,
2:56
S…
Speaker 2 (4- The Iterable Interface)
we ask, do you have more items?
2:58
S…
Speaker 1 (4- The Iterable Interface)
It says yes.
2:59
S…
Speaker 2 (4- The Iterable Interface)
So here we call the next method.
3:02
S…
Speaker 1 (4- The Iterable Interface)
This will return the current item,
3:03
S…
Speaker 1 (4- The Iterable Interface)
which is the A,
3:04
S…
Speaker 2 (4- The Iterable Interface)
and then it will advance the pointer to point to B.
3:08
S…
Speaker 2 (4- The Iterable Interface)
In the next iteration,
3:09
S…
Speaker 1 (4- The Iterable Interface)
we ask the iterator,
3:10
S…
Speaker 2 (4- The Iterable Interface)
do you have more items?
3:12
S…
Speaker 1 (4- The Iterable Interface)
We still do.
3:13
S…
Speaker 2 (4- The Iterable Interface)
So this method will return true.
3:15
S…
Speaker 2 (4- The Iterable Interface)
Then we read the current item and advance the pointer.
3:19
S…
Speaker 1 (4- The Iterable Interface)
One more time,
3:20
S…
Speaker 2 (4- The Iterable Interface)
we read the C, and then the iterator ends up here.
3:23
S…
Speaker 2 (4- The Iterable Interface)
So in the next iteration,
3:24
S…
Speaker 2 (4- The Iterable Interface)
when we call the hasNext method,
3:26
S…
Speaker 1 (4- The Iterable Interface)
we'll get false.
3:27
S…
Speaker 2 (4- The Iterable Interface)
So this iterator is simply an object that we
3:31
S…
Speaker 2 (4- The Iterable Interface)
use to iterate over a list,
3:33
S…
Speaker 1 (4- The Iterable Interface)
or more accurately,
3:34
S…
Speaker 2 (4- The Iterable Interface)
an object that is iterable.
3:36
S…
Speaker 2 (4- The Iterable Interface)
Now let me show you something really interesting.
3:39
S…
Speaker 2 (4- The Iterable Interface)
Let's compile the code,
3:40
S…
Speaker 2 (4- The Iterable Interface)
then from the project window,
3:43
S…
Speaker 2 (4- The Iterable Interface)
let's select the main class and look at its bytecode representation.
3:48
S…
Speaker 1 (4- The Iterable Interface)
Take a look.
3:51
S…
Speaker 2 (4- The Iterable Interface)
So over here we have a reference to iterator that has next
3:55
S…
Speaker 2 (4- The Iterable Interface)
and iterator dot next.
3:57
S…
Speaker 2 (4- The Iterable Interface)
Now I'm going to delete all the code for
4:01
S…
Speaker 2 (4- The Iterable Interface)
using this iterator object and instead use a for each loop.
4:05
S…
Speaker 2 (4- The Iterable Interface)
So for each item and list we're going to print
4:09
S…
Speaker 1 (4- The Iterable Interface)
each item.
4:10
S…
Speaker 2 (4- The Iterable Interface)
One more time we're going to compile the code and
4:14
S…
Speaker 2 (4- The Iterable Interface)
look at the bytecode representation of the main class.
4:17
S…
Speaker 1 (4- The Iterable Interface)
Take a look.
4:20
S…
Speaker 2 (4- The Iterable Interface)
We still have a reference to the iterator object.
4:24
S…
Speaker 2 (4- The Iterable Interface)
So the for each loop is actually a syntactical sugar over
4:28
S…
Speaker 2 (4- The Iterable Interface)
the iterator object.
4:29
S…
Speaker 2 (4- The Iterable Interface)
When we use the for each loop,
4:31
S…
Speaker 2 (4- The Iterable Interface)
the Java compiler will convert our code to use an iterator object.
4:35
S…
Speaker 2 (4- The Iterable Interface)
This is the reason why we can iterate over this list using
4:39
S…
Speaker 1 (4- The Iterable Interface)
a for each loop.
4:40
S…
Speaker 1 (4- The Iterable Interface)
Now,
4:41
S…
Speaker 2 (4- The Iterable Interface)
if I remove this bit of code over here,
4:45
S…
Speaker 2 (4- The Iterable Interface)
implements iterable,
4:46
S…
Speaker 2 (4- The Iterable Interface)
and go back to the main class
4:48
S…
Speaker 2 (4- The Iterable Interface)
you can see we have a compilation error saying for each not applicable to type
4:52
S…
Speaker 2 (4- The Iterable Interface)
generic list.
4:54
S…
Speaker 2 (4- The Iterable Interface)
So this is the benefit of implementing the iterable interface.
4:58
S…
Speaker 2 (4- The Iterable Interface)
It allows us to iterate over an object without knowing anything about
5:02
S…
Speaker 2 (4- The Iterable Interface)
its internal implementation.
5:03
S…
Speaker 2 (4- The Iterable Interface)
So I'm going to bring this back.
5:06
S…
Speaker 2 (4- The Iterable Interface)
Now if tomorrow we decide to replace this array with let's say an array
5:10
S…
Speaker 2 (4- The Iterable Interface)
list, our for each loop is not going to get affected.
5:14
S…
Speaker 2 (4- The Iterable Interface)
Now our implementation is not complete.
5:16
S…
Speaker 2 (4- The Iterable Interface)
We still have to implement the iterator method and that's
5:20
S…
Speaker 1 (4- The Iterable Interface)
what we're going to do next.
Transkrip ini dibuat oleh AI (pengakuan bicara otomatis). Mungkin mengandung kesalahan å verifikasi terhadap audio asli untuk penggunaan kritis. Kebijakan AI
Ringkasan
Klik Summarize untuk membuat ringkasan AI dari transkrip ini.
Ringkasan...
Tanyakan AI Tentang Transcript Ini
Tanyakan apa saja tentang transkrip ini, AI akan menemukan bagian yang relevan dan jawaban.