Только показываю
0:03
S… Speaker 1 (9- The Comparator Interface)
So you have learned how to sort objects by implementing the comparable interface.
0:07
S… Speaker 2 (9- The Comparator Interface)
Now,
0:08
S… Speaker 1 (9- The Comparator Interface)
this implementation is not very flexible because it's always comparing
0:12
S… Speaker 1 (9- The Comparator Interface)
customers by their name.
0:13
S… Speaker 1 (9- The Comparator Interface)
What if tomorrow we introduce new fields like email,
0:16
S… Speaker 1 (9- The Comparator Interface)
address,
0:17
S… Speaker 1 (9- The Comparator Interface)
and so on?
0:18
S… Speaker 1 (9- The Comparator Interface)
We're not going to be able to sort our customers by their email or by their address.
0:22
S… Speaker 1 (9- The Comparator Interface)
That's where the comparator interface comes to the rescue.
0:25
S… Speaker 1 (9- The Comparator Interface)
This interface is very similar to the comparable interface,
0:28
S… Speaker 1 (9- The Comparator Interface)
so it's a generic interface and here we specify the type of objects
0:32
S… Speaker 1 (9- The Comparator Interface)
that should be compared with each other.
0:34
S… Speaker 1 (9- The Comparator Interface)
So we can create a new class called email comparator that compares
0:38
S… Speaker 1 (9- The Comparator Interface)
two customers by their email.
0:40
S… Speaker 1 (9- The Comparator Interface)
Let me show you.
0:40
S… Speaker 1 (9- The Comparator Interface)
First I'm going to introduce a new field here,
0:43
S… Speaker 1 (9- The Comparator Interface)
private string email
0:46
S… Speaker 1 (9- The Comparator Interface)
Now let's initialize this through the constructor.
0:49
S… Speaker 1 (9- The Comparator Interface)
So we press alt and enter and add a constructor parameter.
0:53
S… Speaker 1 (9- The Comparator Interface)
Let's click on refactor.
0:55
S… Speaker 1 (9- The Comparator Interface)
There you go.
0:57
S… Speaker 1 (9- The Comparator Interface)
Now we can initialize the email via the constructor.
1:00
S… Speaker 1 (9- The Comparator Interface)
Of course,
1:01
S… Speaker 1 (9- The Comparator Interface)
our main method has broken because IntelliJ didn't know what to pass
1:05
S… Speaker 1 (9- The Comparator Interface)
for these emails.
1:06
S… Speaker 1 (9- The Comparator Interface)
Let's quickly supply a few values like E3,
1:09
S… Speaker 1 (9- The Comparator Interface)
E2,
1:12
S… Speaker 1 (9- The Comparator Interface)
and E1.
1:14
S… Speaker 1 (9- The Comparator Interface)
Now to compare customers by their email,
1:16
S… Speaker 1 (9- The Comparator Interface)
we're going to create a class that implements the comparator interface.
1:20
S… Speaker 2 (9- The Comparator Interface)
So,
1:21
S… Speaker 1 (9- The Comparator Interface)
in this package,
1:23
S… Speaker 1 (9- The Comparator Interface)
I'm going to add a new class.
1:24
S… Speaker 1 (9- The Comparator Interface)
Let's call this email comparator.
1:27
S… Speaker 1 (9- The Comparator Interface)
Here we should type implements comparator
1:32
S… Speaker 1 (9- The Comparator Interface)
of customer.
1:34
S… Speaker 1 (9- The Comparator Interface)
Let's import the type and then
1:40
S… Speaker 1 (9- The Comparator Interface)
implement this interface.
1:43
S… Speaker 1 (9- The Comparator Interface)
Now as you can see,
1:44
S… Speaker 1 (9- The Comparator Interface)
this interface has quite a few methods,
1:46
S… Speaker 1 (9- The Comparator Interface)
but all of these except the compare method have a default implementation,
1:49
S… Speaker 1 (9- The Comparator Interface)
so we don't have to worry about them.
1:51
S… Speaker 1 (9- The Comparator Interface)
Now let's implement this method.
1:53
S… Speaker 1 (9- The Comparator Interface)
So this method takes two customer objects and returns
1:58
S… Speaker 1 (9- The Comparator Interface)
an integer.
1:58
S… Speaker 1 (9- The Comparator Interface)
The logic we're going to write here is exactly like the logic we wrote when
2:02
S… Speaker 1 (9- The Comparator Interface)
implementing the comparable interface.
2:05
S… Speaker 1 (9- The Comparator Interface)
So if the first object is smaller than the second object,
2:07
S… Speaker 1 (9- The Comparator Interface)
we're going to return a negative value.
2:09
S… Speaker 1 (9- The Comparator Interface)
If it's greater,
2:10
S… Speaker 1 (9- The Comparator Interface)
we're going to return a positive value.
2:12
S… Speaker 1 (9- The Comparator Interface)
And if they're equal,
2:12
S… Speaker 1 (9- The Comparator Interface)
we're going to return zero.
2:14
S… Speaker 2 (9- The Comparator Interface)
Now,
2:15
S… Speaker 1 (9- The Comparator Interface)
here I'm going to get the email of the first
2:19
S… Speaker 1 (9- The Comparator Interface)
customer.
2:19
S… Speaker 1 (9- The Comparator Interface)
Now,
2:20
S… Speaker 1 (9- The Comparator Interface)
because this is a private field,
2:21
S… Speaker 1 (9- The Comparator Interface)
we cannot access it here.
2:22
S… Speaker 1 (9- The Comparator Interface)
So we have to go back to our customer class and create a getter for
2:26
S… Speaker 1 (9- The Comparator Interface)
that field.
2:27
S… Speaker 2 (9- The Comparator Interface)
So,
2:28
S… Speaker 1 (9- The Comparator Interface)
over here,
2:29
S… Speaker 1 (9- The Comparator Interface)
I'll put the caret on the field name and press alt
2:33
S… Speaker 1 (9- The Comparator Interface)
and enter.
2:34
S… Speaker 1 (9- The Comparator Interface)
Let's create a getter and a setter for the email field.
2:38
S… Speaker 1 (9- The Comparator Interface)
That's pretty easy.
2:40
S… Speaker 1 (9- The Comparator Interface)
Now we have this getter,
2:42
S… Speaker 1 (9- The Comparator Interface)
getEmail.
2:43
S… Speaker 1 (9- The Comparator Interface)
So,
2:44
S… Speaker 1 (9- The Comparator Interface)
back to our email comparator.
2:45
S… Speaker 1 (9- The Comparator Interface)
We get the email of the first customer.
2:49
S… Speaker 1 (9- The Comparator Interface)
This returns a string.
2:50
S… Speaker 1 (9- The Comparator Interface)
Now,
2:51
S… Speaker 1 (9- The Comparator Interface)
you know that strings are comparable,
2:53
S… Speaker 1 (9- The Comparator Interface)
so here we can call the compareTo method and pass the email
2:57
S… Speaker 1 (9- The Comparator Interface)
of the second customer.
2:58
S… Speaker 2 (9- The Comparator Interface)
So,
2:59
S… Speaker 1 (9- The Comparator Interface)
O2, let getEmail.
3:00
S… Speaker 1 (9- The Comparator Interface)
And then we return the result.
3:03
S… Speaker 1 (9- The Comparator Interface)
As simple as that.
3:04
S… Speaker 2 (9- The Comparator Interface)
Now,
3:06
S… Speaker 1 (9- The Comparator Interface)
Back to our main method.
3:07
S… Speaker 1 (9- The Comparator Interface)
When calling the sort method,
3:09
S… Speaker 1 (9- The Comparator Interface)
we can pass a comparator object.
3:11
S… Speaker 1 (9- The Comparator Interface)
So the sort method has two overloads,
3:16
S… Speaker 1 (9- The Comparator Interface)
one that takes a list and the other that takes a list along with a
3:20
S… Speaker 1 (9- The Comparator Interface)
comparator.
3:20
S… Speaker 1 (9- The Comparator Interface)
So I'm going to use the second overload and here we pass our
3:25
S… Speaker 1 (9- The Comparator Interface)
email comparator.
3:27
S… Speaker 1 (9- The Comparator Interface)
Now, when we run this program,
3:28
S… Speaker 1 (9- The Comparator Interface)
we're going to see the customer with the name C first,
3:32
S… Speaker 1 (9- The Comparator Interface)
because its email is going to come first.
3:34
S… Speaker 1 (9- The Comparator Interface)
Then we're going to save the customer with the name A,
3:36
S… Speaker 1 (9- The Comparator Interface)
and finally the customer with the name B.
3:39
S… Speaker 1 (9- The Comparator Interface)
Take a look.
3:39
S… Speaker 1 (9- The Comparator Interface)
So we got C,
3:44
S… Speaker 1 (9- The Comparator Interface)
A, and B.
3:45
S… Speaker 1 (9- The Comparator Interface)
So this is how we can use the comparator interface.

Эта запись была составлена АИ (автоматическое распознавание речи). Политика МА

❤️ Любите STT.ai? Расскажите друзьям!
Резюме
Нажмите Нажмите Обобщение для составления резюме этой стенограммы.
Резюмируя...
Спросите AI об этом писце
Спросите что - нибудь об этой стенограмме — МА найдет соответствующие разделы и ответ.