8- The Comparable Interface
Jun 21, 2026 09:49
· 4:28
· English
· Whisper Turbo
· 2 Сөйләүчеләр
Бүгенге көндә бу бинада китапханә эшли.
Кайвакытлы саклау өчен яңарту →
Тик күрсәтү
0:03
S…
Speaker 1 (8- The Comparable Interface)
In this video I'm going to show you how to sort data using the comparable interface.
0:07
S…
Speaker 1 (8- The Comparable Interface)
So first we're going to add a new class to our collections package.
0:11
S…
Speaker 1 (8- The Comparable Interface)
We're going to call that customer.
0:14
S…
Speaker 1 (8- The Comparable Interface)
In this class we're going to add a private field,
0:18
S…
Speaker 1 (8- The Comparable Interface)
private string,
0:19
S…
Speaker 1 (8- The Comparable Interface)
name,
0:19
S…
Speaker 1 (8- The Comparable Interface)
and we want to initialize this from the constructor.
0:23
S…
Speaker 1 (8- The Comparable Interface)
So we press command and N on Mac or control and N
0:27
S…
Speaker 1 (8- The Comparable Interface)
on Windows to bring up the generate palette and select constructor and
0:31
S…
Speaker 1 (8- The Comparable Interface)
then select the name field.
0:33
S…
Speaker 1 (8- The Comparable Interface)
So, we're initializing this field through our constructor.
0:36
S…
Speaker 1 (8- The Comparable Interface)
Super easy.
0:37
S…
Speaker 2 (8- The Comparable Interface)
Now,
0:38
S…
Speaker 1 (8- The Comparable Interface)
back to our main class,
0:40
S…
Speaker 1 (8- The Comparable Interface)
I'm going to create a list of customers.
0:42
S…
Speaker 1 (8- The Comparable Interface)
Let's import these types,
0:44
S…
Speaker 1 (8- The Comparable Interface)
and then set this to a new array
0:48
S…
Speaker 1 (8- The Comparable Interface)
list.
0:49
S…
Speaker 1 (8- The Comparable Interface)
Now let's add three customers to
0:53
S…
Speaker 1 (8- The Comparable Interface)
this list.
0:54
S…
Speaker 1 (8- The Comparable Interface)
So customers .add,
0:55
S…
Speaker 1 (8- The Comparable Interface)
here's one customer.
0:57
S…
Speaker 1 (8- The Comparable Interface)
I want to set the name to B,
0:59
S…
Speaker 1 (8- The Comparable Interface)
another customer with the name A,
1:02
S…
Speaker 1 (8- The Comparable Interface)
and finally the last customer with the name C.
1:06
S…
Speaker 1 (8- The Comparable Interface)
Now, to sort this list,
1:07
S…
Speaker 1 (8- The Comparable Interface)
we can use the collections class.
1:09
S…
Speaker 1 (8- The Comparable Interface)
You learned about this earlier.
1:10
S…
Speaker 1 (8- The Comparable Interface)
So earlier we talked about the addAll method.
1:13
S…
Speaker 1 (8- The Comparable Interface)
Here we have another method for sorting lists.
1:16
S…
Speaker 2 (8- The Comparable Interface)
So,
1:17
S…
Speaker 1 (8- The Comparable Interface)
we can pass the customer's object over here,
1:20
S…
Speaker 1 (8- The Comparable Interface)
but we get a compilation error.
1:23
S…
Speaker 1 (8- The Comparable Interface)
No instances of type variables t exists so that customer confirms
1:27
S…
Speaker 1 (8- The Comparable Interface)
to comparable.
1:28
S…
Speaker 1 (8- The Comparable Interface)
I know this is very confusing.
1:30
S…
Speaker 1 (8- The Comparable Interface)
Basically all this is telling us is that the customer class does not implement
1:35
S…
Speaker 1 (8- The Comparable Interface)
the comparable interface.
1:36
S…
Speaker 1 (8- The Comparable Interface)
Now why does this matter?
1:37
S…
Speaker 1 (8- The Comparable Interface)
Well, a lot of sorting algorithms work by comparing objects.
1:42
S…
Speaker 1 (8- The Comparable Interface)
So if we can compare two objects,
1:44
S…
Speaker 1 (8- The Comparable Interface)
we can determine which object should come first,
1:46
S…
Speaker 1 (8- The Comparable Interface)
which one should come next,
1:47
S…
Speaker 1 (8- The Comparable Interface)
and so on.
1:48
S…
Speaker 1 (8- The Comparable Interface)
So to fix this problem,
1:50
S…
Speaker 1 (8- The Comparable Interface)
we should implement the comparable interface in our customer class and that
1:54
S…
Speaker 1 (8- The Comparable Interface)
is super easy.
1:55
S…
Speaker 1 (8- The Comparable Interface)
You learned this in the last section.
1:56
S…
Speaker 1 (8- The Comparable Interface)
So over here we type implements comparable
2:01
S…
Speaker 1 (8- The Comparable Interface)
of customer because we
2:06
S…
Speaker 1 (8- The Comparable Interface)
want to compare a customer with another customer.
2:08
S…
Speaker 1 (8- The Comparable Interface)
Now we press alt and enter and implement this
2:12
S…
Speaker 1 (8- The Comparable Interface)
interface.
2:13
S…
Speaker 2 (8- The Comparable Interface)
Now,
2:14
S…
Speaker 1 (8- The Comparable Interface)
what should we do in this method?
2:17
S…
Speaker 1 (8- The Comparable Interface)
Let me quickly refresh your memory.
2:18
S…
Speaker 1 (8- The Comparable Interface)
We want to compare two objects.
2:20
S…
Speaker 1 (8- The Comparable Interface)
If this object is smaller or less than the other object,
2:24
S…
Speaker 1 (8- The Comparable Interface)
we should return a negative value,
2:27
S…
Speaker 1 (8- The Comparable Interface)
like minus 1.
2:28
S…
Speaker 1 (8- The Comparable Interface)
It could be anything.
2:29
S…
Speaker 1 (8- The Comparable Interface)
If these two objects are equal,
2:31
S…
Speaker 1 (8- The Comparable Interface)
we should return 0.
2:33
S…
Speaker 1 (8- The Comparable Interface)
And if this object is greater than the other object,
2:36
S…
Speaker 1 (8- The Comparable Interface)
we should return a positive value.
2:38
S…
Speaker 1 (8- The Comparable Interface)
It could be 1 or any positive values.
2:40
S…
Speaker 2 (8- The Comparable Interface)
Now,
2:41
S…
Speaker 1 (8- The Comparable Interface)
on what basis are we going to compare these customers?
2:43
S…
Speaker 1 (8- The Comparable Interface)
Currently,
2:44
S…
Speaker 1 (8- The Comparable Interface)
we only have the name field.
2:45
S…
Speaker 2 (8- The Comparable Interface)
So,
2:47
S…
Speaker 1 (8- The Comparable Interface)
we should compare the name of this customer with the other customer.
2:50
S…
Speaker 2 (8- The Comparable Interface)
However,
2:51
S…
Speaker 1 (8- The Comparable Interface)
we cannot write code like this,
2:53
S…
Speaker 1 (8- The Comparable Interface)
if name is less than o .name.
2:56
S…
Speaker 1 (8- The Comparable Interface)
Because this comparison operator can only be applied between numbers
3:00
S…
Speaker 1 (8- The Comparable Interface)
and characters,
3:01
S…
Speaker 1 (8- The Comparable Interface)
nothing else.
3:02
S…
Speaker 2 (8- The Comparable Interface)
So,
3:03
S…
Speaker 1 (8- The Comparable Interface)
to solve this problem,
3:04
S…
Speaker 1 (8- The Comparable Interface)
we're going to get the name of this customer,
3:07
S…
Speaker 1 (8- The Comparable Interface)
this is a string object,
3:08
S…
Speaker 1 (8- The Comparable Interface)
it's comparable,
3:09
S…
Speaker 1 (8- The Comparable Interface)
so it has the compareTo method.
3:11
S…
Speaker 1 (8- The Comparable Interface)
We can compare this with the name of the other customer,
3:15
S…
Speaker 2 (8- The Comparable Interface)
so o .name.
3:17
S…
Speaker 1 (8- The Comparable Interface)
dot name.
3:17
S…
Speaker 2 (8- The Comparable Interface)
Okay?
3:18
S…
Speaker 1 (8- The Comparable Interface)
Now whatever value this method returns,
3:21
S…
Speaker 1 (8- The Comparable Interface)
we simply return that.
3:22
S…
Speaker 2 (8- The Comparable Interface)
Okay?
3:23
S…
Speaker 2 (8- The Comparable Interface)
Also,
3:25
S…
Speaker 1 (8- The Comparable Interface)
let's rename this parameter to other.
3:27
S…
Speaker 1 (8- The Comparable Interface)
That's better.
3:28
S…
Speaker 1 (8- The Comparable Interface)
So we're done with the implementation of the comparable interface
3:32
S…
Speaker 1 (8- The Comparable Interface)
in our customer class.
3:33
S…
Speaker 1 (8- The Comparable Interface)
Now back to the main method.
3:35
S…
Speaker 1 (8- The Comparable Interface)
The error is gone.
3:36
S…
Speaker 1 (8- The Comparable Interface)
So our list should be sorted.
3:38
S…
Speaker 1 (8- The Comparable Interface)
Let's verify it by printing the customer's list on the
3:42
S…
Speaker 1 (8- The Comparable Interface)
console. We get this weird strain because
3:47
S…
Speaker 1 (8- The Comparable Interface)
This is the default representation of a customer object as a string.
3:51
S…
Speaker 1 (8- The Comparable Interface)
We talked about this many times before.
3:53
S…
Speaker 1 (8- The Comparable Interface)
So to solve this problem,
3:55
S…
Speaker 1 (8- The Comparable Interface)
we should overwrite the toString method in this class.
3:58
S…
Speaker 1 (8- The Comparable Interface)
That is super easy.
3:59
S…
Speaker 1 (8- The Comparable Interface)
We bring up the generate palette and select toString.
4:04
S…
Speaker 1 (8- The Comparable Interface)
Now I'm going to simplify this
4:08
S…
Speaker 1 (8- The Comparable Interface)
expression.
4:08
S…
Speaker 1 (8- The Comparable Interface)
We only want to return the name of this customer when
4:12
S…
Speaker 1 (8- The Comparable Interface)
we try to convert it to a string.
4:14
S…
Speaker 1 (8- The Comparable Interface)
Let's run our program one more time.
4:19
S…
Speaker 1 (8- The Comparable Interface)
As you can see,
4:19
S…
Speaker 1 (8- The Comparable Interface)
our list is sorted.
4:21
S…
Speaker 1 (8- The Comparable Interface)
Beautiful.
4:21
S…
Speaker 1 (8- The Comparable Interface)
In the next video,
4:22
S…
Speaker 1 (8- The Comparable Interface)
we're going to talk about another interface that is used for sorting data.
Бу транскрипция уйлап табылган (автомат сөйләм таныу) ярдәмендә ясалган. Анда хаталар булырга мөмкин - мөһим куллану өчен оригинал аудио белән тикшерегез. AI сәясәт
Тиздән
Бу терминның берничә мәгънәсе бар: Транссибирь тимер юлының Транссибирь бүлекчәсе
Тиздән...
Бу транскрипция турында АА-дан сорагыз
Бу ысулның төп өстенлеге — ул җөмләнең эчтәлеген һәм аның эчтәлеген аңлатучы җөмләләрне аерып күрсәтә.