6- The Collection Interface
Jun 21, 2026 09:22
· 9:35
· English
· Whisper Turbo
· 2 Hátalar
Ūessi afritunarskrá rennur út í dag.
Uppfærsla fyrir varanlega geymslu →
Sýna aðeins
0:03
S…
Speaker 2 (6- The Collection Interface)
The next interface we're going to talk about is the collection interface,
0:06
S…
Speaker 2 (6- The Collection Interface)
which represents an object that acts as a collection or
0:10
S…
Speaker 2 (6- The Collection Interface)
a container for other objects.
0:12
S…
Speaker 2 (6- The Collection Interface)
What are the operations that we need to work with a collection?
0:15
S…
Speaker 2 (6- The Collection Interface)
We should be able to add an object to this collection,
0:18
S…
Speaker 2 (6- The Collection Interface)
we should be able to remove an existing object,
0:20
S…
Speaker 2 (6- The Collection Interface)
we should be able to check for the existence of an object and so on.
0:23
S…
Speaker 2 (6- The Collection Interface)
These are the operations that the collection interface declares.
0:27
S…
Speaker 2 (6- The Collection Interface)
Here on the documentation you can see that the collection interface is also a generic
0:31
S…
Speaker 2 (6- The Collection Interface)
interface because here we have a generic type parameter,
0:34
S…
Speaker 2 (6- The Collection Interface)
but instead of t we have e which is short for element that
0:38
S…
Speaker 2 (6- The Collection Interface)
is a common convention for declaring interfaces or classes that have a collection
0:43
S…
Speaker 2 (6- The Collection Interface)
semantic because a collection can have multiple elements,
0:46
S…
Speaker 1 (6- The Collection Interface)
okay?
0:47
S…
Speaker 2 (6- The Collection Interface)
Now, over here you can see that this interface extends the iterable
0:51
S…
Speaker 2 (6- The Collection Interface)
interface, so every collection is also iterable.
0:54
S…
Speaker 2 (6- The Collection Interface)
And that means we can use a forage loop to iterate over all collections
0:59
S…
Speaker 2 (6- The Collection Interface)
in Java.
1:00
S…
Speaker 2 (6- The Collection Interface)
Now over here you can see various classes that implement this interface.
1:03
S…
Speaker 1 (6- The Collection Interface)
As you can see,
1:04
S…
Speaker 2 (6- The Collection Interface)
there are over 20 implementations of this interface.
1:07
S…
Speaker 1 (6- The Collection Interface)
Most of the time,
1:08
S…
Speaker 2 (6- The Collection Interface)
like 90 % of the time,
1:09
S…
Speaker 2 (6- The Collection Interface)
you only need to use the ArrayList class.
1:12
S…
Speaker 2 (6- The Collection Interface)
These other implementations are for special purposes and we're not going
1:16
S…
Speaker 2 (6- The Collection Interface)
to cover them in this course.
1:17
S…
Speaker 2 (6- The Collection Interface)
You can learn about them on your own.
1:18
S…
Speaker 2 (6- The Collection Interface)
I just want to give you a strong foundation.
1:21
S…
Speaker 2 (6- The Collection Interface)
So back to our project,
1:23
S…
Speaker 2 (6- The Collection Interface)
I'm going to add a new package,
1:25
S…
Speaker 2 (6- The Collection Interface)
a new package called collections.
1:28
S…
Speaker 2 (6- The Collection Interface)
We're going to write all the code in this section.
1:30
S…
Speaker 2 (6- The Collection Interface)
in this package.
1:31
S…
Speaker 2 (6- The Collection Interface)
I'm going to add a new class called collections demo.
1:36
S…
Speaker 2 (6- The Collection Interface)
In this class we're going to have a public static method called
1:41
S…
Speaker 1 (6- The Collection Interface)
show.
1:41
S…
Speaker 2 (6- The Collection Interface)
This is where I'm going to show you various operations in the collection interface.
1:45
S…
Speaker 2 (6- The Collection Interface)
If you download the source code that I've included at the beginning of this course,
1:49
S…
Speaker 2 (6- The Collection Interface)
you can see all examples in this class.
1:52
S…
Speaker 2 (6- The Collection Interface)
But while I'm recording this video,
1:54
S…
Speaker 2 (6- The Collection Interface)
I'm going to delete some of these examples to keep the screen clean and easy to read.
1:59
S…
Speaker 2 (6- The Collection Interface)
Now back in the main class,
2:00
S…
Speaker 2 (6- The Collection Interface)
we're going to call collections demo dot
2:05
S…
Speaker 2 (6- The Collection Interface)
show.
2:06
S…
Speaker 2 (6- The Collection Interface)
Now here I'm going to declare a variable of type collection
2:10
S…
Speaker 2 (6- The Collection Interface)
of string.
2:11
S…
Speaker 2 (6- The Collection Interface)
Let's import this type.
2:14
S…
Speaker 2 (6- The Collection Interface)
So that is an interface.
2:16
S…
Speaker 2 (6- The Collection Interface)
We call this variable collection.
2:18
S…
Speaker 2 (6- The Collection Interface)
Now here we should set this to a new instance of ArrayList
2:22
S…
Speaker 2 (6- The Collection Interface)
because this is one of the classes that implements
2:26
S…
Speaker 2 (6- The Collection Interface)
this interface.
2:27
S…
Speaker 2 (6- The Collection Interface)
Now let's add a few items to this collection.
2:30
S…
Speaker 2 (6- The Collection Interface)
So collection dot add.
2:32
S…
Speaker 2 (6- The Collection Interface)
can add a string like A,
2:34
S…
Speaker 1 (6- The Collection Interface)
then B,
2:35
S…
Speaker 2 (6- The Collection Interface)
and then C.
2:36
S…
Speaker 2 (6- The Collection Interface)
Because this collection is iterable,
2:39
S…
Speaker 2 (6- The Collection Interface)
we can use a for each loop here.
2:40
S…
Speaker 2 (6- The Collection Interface)
So for item in collection,
2:43
S…
Speaker 2 (6- The Collection Interface)
we can print each item.
2:45
S…
Speaker 1 (6- The Collection Interface)
So we get ABC.
2:48
S…
Speaker 2 (6- The Collection Interface)
We can also print the collection as a whole.
2:51
S…
Speaker 2 (6- The Collection Interface)
So print collection.
2:53
S…
Speaker 1 (6- The Collection Interface)
Now,
2:56
S…
Speaker 2 (6- The Collection Interface)
what if you want to add multiple items in one go,
2:58
S…
Speaker 2 (6- The Collection Interface)
instead of calling the add method three times?
3:00
S…
Speaker 1 (6- The Collection Interface)
Well,
3:02
S…
Speaker 2 (6- The Collection Interface)
Here we have another class called collections.
3:04
S…
Speaker 2 (6- The Collection Interface)
That is a utility class declared in the java .util package.
3:08
S…
Speaker 2 (6- The Collection Interface)
This class has a method called addAll.
3:12
S…
Speaker 2 (6- The Collection Interface)
The first parameter of this method is the collection that we want to modify.
3:16
S…
Speaker 2 (6- The Collection Interface)
So we pass our collection over here.
3:17
S…
Speaker 2 (6- The Collection Interface)
Now look at the second argument.
3:19
S…
Speaker 2 (6- The Collection Interface)
Here we can see the list of parameters by pressing command and P
3:24
S…
Speaker 2 (6- The Collection Interface)
on Mac or control and P on Windows.
3:26
S…
Speaker 2 (6- The Collection Interface)
If this shortcut doesn't work for you,
3:28
S…
Speaker 2 (6- The Collection Interface)
you can find it under view
3:31
S…
Speaker 2 (6- The Collection Interface)
parameter info.
3:32
S…
Speaker 2 (6- The Collection Interface)
So look at the second parameter,
3:34
S…
Speaker 2 (6- The Collection Interface)
string dot dot dot.
3:36
S…
Speaker 1 (6- The Collection Interface)
When you see dot dot dot,
3:38
S…
Speaker 2 (6- The Collection Interface)
that means you can pass a variable number of arguments.
3:41
S…
Speaker 2 (6- The Collection Interface)
You can pass zero or more strings.
3:43
S…
Speaker 2 (6- The Collection Interface)
So here we can pass A and then B
3:47
S…
Speaker 2 (6- The Collection Interface)
and then C.
3:48
S…
Speaker 2 (6- The Collection Interface)
So instead of calling the add method three times,
3:51
S…
Speaker 2 (6- The Collection Interface)
we're passing multiple arguments to this method.
3:54
S…
Speaker 1 (6- The Collection Interface)
Now,
3:55
S…
Speaker 2 (6- The Collection Interface)
if we run our program,
3:56
S…
Speaker 2 (6- The Collection Interface)
we get the same result as before.
3:59
S…
Speaker 2 (6- The Collection Interface)
Another useful method is the size method that returns the size of a collection.
4:03
S…
Speaker 2 (6- The Collection Interface)
So here we have three items.
4:07
S…
Speaker 2 (6- The Collection Interface)
We also have a method for removing an object
4:11
S…
Speaker 2 (6- The Collection Interface)
from a collection.
4:12
S…
Speaker 2 (6- The Collection Interface)
So we can call collection .removeA and
4:16
S…
Speaker 2 (6- The Collection Interface)
then we can print the collection.
4:18
S…
Speaker 1 (6- The Collection Interface)
So A is gone.
4:22
S…
Speaker 2 (6- The Collection Interface)
We can also remove all items by calling
4:26
S…
Speaker 2 (6- The Collection Interface)
the clear method.
4:27
S…
Speaker 2 (6- The Collection Interface)
Now the collection is empty.
4:31
S…
Speaker 2 (6- The Collection Interface)
We can verify that by calling the is empty
4:35
S…
Speaker 1 (6- The Collection Interface)
method.
4:35
S…
Speaker 1 (6- The Collection Interface)
So
4:37
S…
Speaker 2 (6- The Collection Interface)
it returns true.
4:38
S…
Speaker 2 (6- The Collection Interface)
If you want to check for the existence of an item,
4:41
S…
Speaker 2 (6- The Collection Interface)
you can call the contains method.
4:45
S…
Speaker 2 (6- The Collection Interface)
So we want to see if this collection contains an
4:49
S…
Speaker 1 (6- The Collection Interface)
A.
4:50
S…
Speaker 2 (6- The Collection Interface)
So contains A,
4:51
S…
Speaker 2 (6- The Collection Interface)
and then we can print contains A.
4:54
S…
Speaker 1 (6- The Collection Interface)
Get true.
4:57
S…
Speaker 2 (6- The Collection Interface)
There are times you want to convert a collection to a regular
5:01
S…
Speaker 1 (6- The Collection Interface)
array.
5:01
S…
Speaker 2 (6- The Collection Interface)
So here we call collection .toArray
5:06
S…
Speaker 1 (6- The Collection Interface)
As you can see,
5:07
S…
Speaker 2 (6- The Collection Interface)
this method has three forms or three overloads.
5:10
S…
Speaker 2 (6- The Collection Interface)
If you don't pass any arguments here,
5:12
S…
Speaker 2 (6- The Collection Interface)
this will return an object array.
5:14
S…
Speaker 2 (6- The Collection Interface)
So we can store it in an object array.
5:18
S…
Speaker 2 (6- The Collection Interface)
So every item in this array is going to be an instance
5:22
S…
Speaker 2 (6- The Collection Interface)
of the object class.
5:23
S…
Speaker 2 (6- The Collection Interface)
That means if we get the first item and
5:27
S…
Speaker 2 (6- The Collection Interface)
use the dot operator,
5:29
S…
Speaker 2 (6- The Collection Interface)
we don't see any of the string methods.
5:30
S…
Speaker 2 (6- The Collection Interface)
If that's what you need,
5:32
S…
Speaker 2 (6- The Collection Interface)
then you need to convert this collection to a string array instead of an object array.
5:36
S…
Speaker 2 (6- The Collection Interface)
That's very easy.
5:37
S…
Speaker 2 (6- The Collection Interface)
We call collection .toArray.
5:40
S…
Speaker 2 (6- The Collection Interface)
We're going to use this form,
5:42
S…
Speaker 2 (6- The Collection Interface)
this overload that takes a tArray.
5:45
S…
Speaker 2 (6- The Collection Interface)
So if we pass a new string array
5:49
S…
Speaker 2 (6- The Collection Interface)
here, how many items do we have in this collection?
5:51
S…
Speaker 1 (6- The Collection Interface)
1,
5:52
S…
Speaker 2 (6- The Collection Interface)
2, 3.
5:53
S…
Speaker 2 (6- The Collection Interface)
So we pass a string array over here,
5:56
S…
Speaker 2 (6- The Collection Interface)
and this will return a string array.
6:01
S…
Speaker 2 (6- The Collection Interface)
Also, we don't really have to specify the size here.
6:04
S…
Speaker 2 (6- The Collection Interface)
We could simply pass 0 and that is a common convention because
6:08
S…
Speaker 2 (6- The Collection Interface)
you don't want to worry about counting the number of items in your collection.
6:10
S…
Speaker 2 (6- The Collection Interface)
You can simply pass an empty array and this method would automatically
6:14
S…
Speaker 2 (6- The Collection Interface)
create an array with enough capacity to hold all these items.
6:19
S…
Speaker 1 (6- The Collection Interface)
Now,
6:20
S…
Speaker 2 (6- The Collection Interface)
if we get the first item,
6:22
S…
Speaker 2 (6- The Collection Interface)
we can access all the string methods.
6:26
S…
Speaker 2 (6- The Collection Interface)
For example,
6:26
S…
Speaker 2 (6- The Collection Interface)
we can convert the string to the lowercase or uppercase and so on.
6:31
S…
Speaker 1 (6- The Collection Interface)
And by the way,
6:32
S…
Speaker 2 (6- The Collection Interface)
we can use var here,
6:33
S…
Speaker 2 (6- The Collection Interface)
just as usual.
6:34
S…
Speaker 2 (6- The Collection Interface)
Now some people don't like var,
6:36
S…
Speaker 2 (6- The Collection Interface)
they argue that we cannot see the type of this object,
6:39
S…
Speaker 2 (6- The Collection Interface)
but that is not right.
6:40
S…
Speaker 2 (6- The Collection Interface)
So if you put the caret over here,
6:42
S…
Speaker 2 (6- The Collection Interface)
and then go to view,
6:44
S…
Speaker 2 (6- The Collection Interface)
quick documentation,
6:46
S…
Speaker 2 (6- The Collection Interface)
the shortcut is F1,
6:47
S…
Speaker 2 (6- The Collection Interface)
you can easily see that this is an object array.
6:51
S…
Speaker 2 (6- The Collection Interface)
Or if you put the caret over here,
6:53
S…
Speaker 1 (6- The Collection Interface)
and one more time,
6:54
S…
Speaker 2 (6- The Collection Interface)
press F1,
6:55
S…
Speaker 2 (6- The Collection Interface)
you can see that this is a string array.
6:57
S…
Speaker 2 (6- The Collection Interface)
So I personally prefer var because it makes our code cleaner.
7:02
S…
Speaker 2 (6- The Collection Interface)
So this is how we can use the two array methods.
7:04
S…
Speaker 1 (6- The Collection Interface)
Now,
7:05
S…
Speaker 2 (6- The Collection Interface)
there are times you want to compare two collections for equality.
7:09
S…
Speaker 2 (6- The Collection Interface)
So I'm going to create another collection of type
7:13
S…
Speaker 1 (6- The Collection Interface)
string.
7:13
S…
Speaker 2 (6- The Collection Interface)
Let's call it other and set it to a new ArrayList.
7:18
S…
Speaker 2 (6- The Collection Interface)
Now what would happen if we use var
7:22
S…
Speaker 2 (6- The Collection Interface)
over here?
7:23
S…
Speaker 2 (6- The Collection Interface)
Then the type of this variable is going to be the ArrayList class,
7:26
S…
Speaker 2 (6- The Collection Interface)
not the collection interface.
7:28
S…
Speaker 2 (6- The Collection Interface)
So it's going to include the additional methods that are declared in the ArrayList
7:32
S…
Speaker 1 (6- The Collection Interface)
class.
7:33
S…
Speaker 2 (6- The Collection Interface)
Now, as I explained in the second part of this series,
7:35
S…
Speaker 2 (6- The Collection Interface)
we should program against interfaces.
7:37
S…
Speaker 2 (6- The Collection Interface)
This will make our applications more flexible and loosely coupled.
7:41
S…
Speaker 1 (6- The Collection Interface)
So,
7:42
S…
Speaker 2 (6- The Collection Interface)
I'm going to revert this back to collection of string.
7:45
S…
Speaker 1 (6- The Collection Interface)
Now,
7:46
S…
Speaker 2 (6- The Collection Interface)
I want to add every object that we have in this collection in
7:50
S…
Speaker 2 (6- The Collection Interface)
our second collection.
7:51
S…
Speaker 2 (6- The Collection Interface)
So, we type other that add all.
7:54
S…
Speaker 2 (6- The Collection Interface)
Now look at the type of this parameter.
7:58
S…
Speaker 2 (6- The Collection Interface)
It's collection of unknown extends string.
8:02
S…
Speaker 2 (6- The Collection Interface)
So we have a wild card again.
8:03
S…
Speaker 2 (6- The Collection Interface)
That means we can pass a collection of string or any type
8:07
S…
Speaker 2 (6- The Collection Interface)
that extends the collection of string.
8:09
S…
Speaker 1 (6- The Collection Interface)
So,
8:10
S…
Speaker 2 (6- The Collection Interface)
I'm going to pass our first collection.
8:12
S…
Speaker 1 (6- The Collection Interface)
Now,
8:14
S…
Speaker 2 (6- The Collection Interface)
you want to check to see if these two collections are equal.
8:17
S…
Speaker 2 (6- The Collection Interface)
So I'm going to do a print statement here.
8:19
S…
Speaker 2 (6- The Collection Interface)
What do you think is the result of this expression?
8:23
S…
Speaker 2 (6- The Collection Interface)
Collection equals other.
8:25
S…
Speaker 2 (6- The Collection Interface)
Are we going to get true or false?
8:27
S…
Speaker 2 (6- The Collection Interface)
We're going to get false because these are two different objects in memory.
8:31
S…
Speaker 2 (6- The Collection Interface)
Look, here we have two new operations.
8:35
S…
Speaker 2 (6- The Collection Interface)
So these are going to be different objects in memory and this equality
8:39
S…
Speaker 2 (6- The Collection Interface)
operator compares these objects by their references or their addresses
8:43
S…
Speaker 1 (6- The Collection Interface)
in memory.
8:44
S…
Speaker 2 (6- The Collection Interface)
Now if you want to check to see if two collections have the exact same content,
8:48
S…
Speaker 2 (6- The Collection Interface)
you should use the equals method.
8:50
S…
Speaker 2 (6- The Collection Interface)
So print collection equals other.
8:54
S…
Speaker 1 (6- The Collection Interface)
Take a look.
8:57
S…
Speaker 2 (6- The Collection Interface)
So we get false as a result of evaluating this expression because these
9:02
S…
Speaker 2 (6- The Collection Interface)
are two different objects in memory,
9:03
S…
Speaker 2 (6- The Collection Interface)
but we get true because these two collections have the exact same
9:07
S…
Speaker 1 (6- The Collection Interface)
content.
9:10
S…
Speaker 2 (6- The Collection Interface)
So this was all about the collection interface.
9:12
S…
Speaker 1 (6- The Collection Interface)
Once again,
9:13
S…
Speaker 2 (6- The Collection Interface)
don't worry about memorizing any of these.
9:15
S…
Speaker 2 (6- The Collection Interface)
All you need to take away from this video is the kind of operations that are provided
9:19
S…
Speaker 2 (6- The Collection Interface)
by the collection interface.
9:21
S…
Speaker 2 (6- The Collection Interface)
We can add an object,
9:22
S…
Speaker 2 (6- The Collection Interface)
we can remove an object,
9:23
S…
Speaker 2 (6- The Collection Interface)
we can check to see if an object exists and so on.
9:26
S…
Speaker 2 (6- The Collection Interface)
Don't worry about memorizing,
9:27
S…
Speaker 2 (6- The Collection Interface)
you can always look at these operations on the documentation.
9:30
S…
Speaker 2 (6- The Collection Interface)
Next we're going to look at the list interface.
Þessi afritun var búin til af AI (sjálfvirk talgreining). Getur innihaldið villur - staðfesta gegn upprunalegu hljóðinu til mikilvægrar notkunar. AI stefna
Samantekt
Smelltu á Samantekt til að búa til AI samantekt á þessari afritun.
Samantekt...
Spyrja AI um þessa afritun
Spyrðu eitthvað um þetta afritunarrit - AI mun finna viðeigandi hluta og svara.