11- The Set Interface
Jun 21, 2026 10:34
· 5:33
· English
· Whisper Turbo
· 2 Speakers
Transkrip iki bakal kadaluwarsa dina iki.
Ngoptimalake kanggo panyimpenan permanen →
Hanya ditampilake
0:03
S…
Speaker 2 (11- The Set Interface)
Now let's talk about the set interface.
0:05
S…
Speaker 2 (11- The Set Interface)
This interface represents a collection that contains no duplicates.
0:09
S…
Speaker 2 (11- The Set Interface)
So it's a great way for storing a list of unique values.
0:13
S…
Speaker 1 (11- The Set Interface)
Let me show you.
0:13
S…
Speaker 2 (11- The Set Interface)
So here I'm going to declare a variable of type set of string.
0:17
S…
Speaker 2 (11- The Set Interface)
Let's import this.
0:19
S…
Speaker 2 (11- The Set Interface)
And we set this to a new hash set.
0:22
S…
Speaker 2 (11- The Set Interface)
This is one of the implementations of the set interface that you use 90 %
0:27
S…
Speaker 1 (11- The Set Interface)
of the time.
0:27
S…
Speaker 2 (11- The Set Interface)
Now we can add a few items to this set.
0:30
S…
Speaker 2 (11- The Set Interface)
So set .add,
0:31
S…
Speaker 1 (11- The Set Interface)
I'm going to add sky.
0:34
S…
Speaker 2 (11- The Set Interface)
then is blue.
0:37
S…
Speaker 2 (11- The Set Interface)
Now I want to duplicate this line so we have two blues.
0:40
S…
Speaker 2 (11- The Set Interface)
Let's see what happens when we print the set.
0:43
S…
Speaker 1 (11- The Set Interface)
The duplicate
0:47
S…
Speaker 1 (11- The Set Interface)
is gone.
0:47
S…
Speaker 2 (11- The Set Interface)
But look at the order of items.
0:49
S…
Speaker 2 (11- The Set Interface)
We have sky blue is.
0:52
S…
Speaker 2 (11- The Set Interface)
So the set interface does not guarantee the order of items.
0:55
S…
Speaker 2 (11- The Set Interface)
That is one of the important characteristics of the set interface you need to understand.
0:59
S…
Speaker 1 (11- The Set Interface)
Do not rely on the order.
1:00
S…
Speaker 2 (11- The Set Interface)
A set only guarantees the uniqueness.
1:03
S…
Speaker 1 (11- The Set Interface)
Okay?
1:04
S…
Speaker 2 (11- The Set Interface)
Now, let's say we have a collection of items and we want to remove the duplicates.
1:08
S…
Speaker 2 (11- The Set Interface)
Using a set,
1:09
S…
Speaker 2 (11- The Set Interface)
we can do it in one line.
1:10
S…
Speaker 1 (11- The Set Interface)
Let me show you.
1:11
S…
Speaker 2 (11- The Set Interface)
So I'm going to delete all this code.
1:12
S…
Speaker 1 (11- The Set Interface)
Let's create a collection
1:17
S…
Speaker 2 (11- The Set Interface)
of string,
1:18
S…
Speaker 1 (11- The Set Interface)
call it collection,
1:19
S…
Speaker 2 (11- The Set Interface)
and set it to a new array list.
1:22
S…
Speaker 1 (11- The Set Interface)
Now in this collection,
1:24
S…
Speaker 2 (11- The Set Interface)
we want to add a few items.
1:25
S…
Speaker 2 (11- The Set Interface)
So I'm going to use the collections class and call
1:29
S…
Speaker 2 (11- The Set Interface)
the addAll method.
1:30
S…
Speaker 2 (11- The Set Interface)
Here we pass our collection along with a
1:34
S…
Speaker 2 (11- The Set Interface)
few strings.
1:35
S…
Speaker 1 (11- The Set Interface)
A, B,
1:37
S…
Speaker 1 (11- The Set Interface)
C and C.
1:38
S…
Speaker 1 (11- The Set Interface)
So we have duplicates.
1:39
S…
Speaker 2 (11- The Set Interface)
Now we can remove the duplicates by only one line of code.
1:43
S…
Speaker 2 (11- The Set Interface)
So all we have to do is to put this collection in a set.
1:46
S…
Speaker 2 (11- The Set Interface)
So we declare a set of string and
1:50
S…
Speaker 2 (11- The Set Interface)
set it to a new high set.
1:53
S…
Speaker 2 (11- The Set Interface)
Now look at the parameters of the constructor of the high set class.
1:57
S…
Speaker 2 (11- The Set Interface)
So this constructor is overloaded three times.
1:59
S…
Speaker 2 (11- The Set Interface)
Look at this overload.
2:01
S…
Speaker 2 (11- The Set Interface)
here we can pass a collection of unknown extends string.
2:06
S…
Speaker 2 (11- The Set Interface)
So once again,
2:07
S…
Speaker 2 (11- The Set Interface)
we have a wild card here and that means we can pass a collection of string or
2:11
S…
Speaker 2 (11- The Set Interface)
any type that extends the collection interface.
2:14
S…
Speaker 1 (11- The Set Interface)
For example,
2:14
S…
Speaker 2 (11- The Set Interface)
we can pass a list of string or a queue of string or another
2:19
S…
Speaker 1 (11- The Set Interface)
set.
2:19
S…
Speaker 2 (11- The Set Interface)
So I'm going to pass our collection over here and
2:23
S…
Speaker 2 (11- The Set Interface)
now if we print the set,
2:26
S…
Speaker 2 (11- The Set Interface)
you can see that the duplicates
2:30
S…
Speaker 1 (11- The Set Interface)
are gone.
2:31
S…
Speaker 2 (11- The Set Interface)
So that's a very cool technique.
2:32
S…
Speaker 2 (11- The Set Interface)
All we have to do is to put a list or a collection inside a set and
2:37
S…
Speaker 2 (11- The Set Interface)
the duplicates are gone.
2:38
S…
Speaker 1 (11- The Set Interface)
Now,
2:39
S…
Speaker 2 (11- The Set Interface)
let's talk about the set operations.
2:41
S…
Speaker 2 (11- The Set Interface)
So I want to create a new set,
2:44
S…
Speaker 2 (11- The Set Interface)
set of string,
2:46
S…
Speaker 2 (11- The Set Interface)
let's call it set one.
2:47
S…
Speaker 2 (11- The Set Interface)
Here we set this to a new high set.
2:50
S…
Speaker 1 (11- The Set Interface)
Now,
2:52
S…
Speaker 2 (11- The Set Interface)
this time instead of a collection,
2:54
S…
Speaker 2 (11- The Set Interface)
I want to pass a list of string.
2:55
S…
Speaker 2 (11- The Set Interface)
But we don't have a list yet.
2:57
S…
Speaker 2 (11- The Set Interface)
One way is to create a list over here.
3:01
S…
Speaker 2 (11- The Set Interface)
But I'm going to show you a better way,
3:02
S…
Speaker 2 (11- The Set Interface)
a faster way.
3:03
S…
Speaker 2 (11- The Set Interface)
We have this class called arrays.
3:05
S…
Speaker 2 (11- The Set Interface)
This is also declared in the java .util package.
3:10
S…
Speaker 2 (11- The Set Interface)
This class has a static method as list.
3:13
S…
Speaker 2 (11- The Set Interface)
Now look at the parameter of this method.
3:16
S…
Speaker 2 (11- The Set Interface)
We have string dot dot dot.
3:18
S…
Speaker 2 (11- The Set Interface)
As I told you before,
3:19
S…
Speaker 2 (11- The Set Interface)
this is called var args,
3:21
S…
Speaker 2 (11- The Set Interface)
which represents a variable number of arguments.
3:23
S…
Speaker 2 (11- The Set Interface)
So here we can pass zero or more arguments.
3:26
S…
Speaker 2 (11- The Set Interface)
I'm going to pass three items,
3:29
S…
Speaker 1 (11- The Set Interface)
a, b,
3:29
S…
Speaker 2 (11- The Set Interface)
and c.
3:31
S…
Speaker 2 (11- The Set Interface)
So this method will return a list of strings initialized with
3:35
S…
Speaker 2 (11- The Set Interface)
these three items.
3:36
S…
Speaker 2 (11- The Set Interface)
Okay, let me put this on a new line so we
3:40
S…
Speaker 2 (11- The Set Interface)
can see clearly, that is better.
3:41
S…
Speaker 2 (11- The Set Interface)
Now I'm going to create another set but with different
3:46
S…
Speaker 1 (11- The Set Interface)
items.
3:46
S…
Speaker 1 (11- The Set Interface)
So B,
3:48
S…
Speaker 1 (11- The Set Interface)
C,
3:49
S…
Speaker 1 (11- The Set Interface)
and D.
3:51
S…
Speaker 2 (11- The Set Interface)
And we're going to call this set 2.
3:54
S…
Speaker 2 (11- The Set Interface)
Now when it comes to sets,
3:56
S…
Speaker 2 (11- The Set Interface)
there are four operations that you need to understand.
3:58
S…
Speaker 2 (11- The Set Interface)
One of them is the union operation,
4:01
S…
Speaker 2 (11- The Set Interface)
which is the combination of two sets.
4:04
S…
Speaker 2 (11- The Set Interface)
So if I want to combine these two sets,
4:06
S…
Speaker 2 (11- The Set Interface)
I can write set one dot add all.
4:09
S…
Speaker 2 (11- The Set Interface)
Now look at this parameter.
4:11
S…
Speaker 2 (11- The Set Interface)
It's a collection of string.
4:13
S…
Speaker 2 (11- The Set Interface)
Here we have a wildcard,
4:15
S…
Speaker 2 (11- The Set Interface)
so we can pass a list of string,
4:16
S…
Speaker 2 (11- The Set Interface)
a set of string,
4:17
S…
Speaker 2 (11- The Set Interface)
or a queue of string.
4:18
S…
Speaker 2 (11- The Set Interface)
So we can add set two,
4:21
S…
Speaker 2 (11- The Set Interface)
and now if we print set one,
4:27
S…
Speaker 2 (11- The Set Interface)
You can see that it has all the items in both sets without any
4:31
S…
Speaker 1 (11- The Set Interface)
duplicates.
4:31
S…
Speaker 2 (11- The Set Interface)
So this is the union operation.
4:33
S…
Speaker 2 (11- The Set Interface)
Another useful operation
4:38
S…
Speaker 2 (11- The Set Interface)
is intersection.
4:39
S…
Speaker 2 (11- The Set Interface)
And that will give us the items that are common in both sets.
4:44
S…
Speaker 2 (11- The Set Interface)
To get the intersection,
4:45
S…
Speaker 2 (11- The Set Interface)
we call the retain all method.
4:48
S…
Speaker 2 (11- The Set Interface)
So this method will retain or keep all the items in set two and
4:52
S…
Speaker 2 (11- The Set Interface)
remove everything else from set one.
4:54
S…
Speaker 1 (11- The Set Interface)
Take a look.
4:58
S…
Speaker 2 (11- The Set Interface)
So the intersection of these two sets is B and C.
5:01
S…
Speaker 2 (11- The Set Interface)
Another useful operation is difference.
5:06
S…
Speaker 2 (11- The Set Interface)
You want to know what items we have
5:10
S…
Speaker 2 (11- The Set Interface)
in the first set that we do not have in the second set.
5:13
S…
Speaker 2 (11- The Set Interface)
To do that we call the remove all method.
5:16
S…
Speaker 2 (11- The Set Interface)
So we are removing everything that we have in set two and keeping the rest.
5:20
S…
Speaker 1 (11- The Set Interface)
Take a look.
5:20
S…
Speaker 1 (11- The Set Interface)
So the result
5:25
S…
Speaker 2 (11- The Set Interface)
is A.
5:25
S…
Speaker 2 (11- The Set Interface)
So this is how we can use sets.
5:28
S…
Speaker 2 (11- The Set Interface)
In the next video we're going to talk about maps.
Transkripsi ieu dihasilkeun ku AI (ngartos basa otomatis). Bisa aya kasalahan - verifikasi kana audio asli pikeun kaperluan kritis. Kebijakan AI
Ringkasan
Klik Ringkasan kanggo nyipta ringkasan AI saka transkripsi iki.
Mengumpulkan...
Tanya AI Ngendi iki Transkrip
Saben wangun wangun bisa digolongake dadi: wangun umum wangun khusus