8- Comparable Interface
Jun 20, 2026 06:18
· 5:38
· English
· Whisper Turbo
· 2 speakers
Dit transcript verloopt in 24 dagen.
Upgrade voor permanente opslag →
Uitsluitend tonen
0:03
S…
Speaker 1 (8- Comparable Interface)
Earlier I briefly mentioned the comparable interface.
0:06
S…
Speaker 1 (8- Comparable Interface)
We use this interface to compare two objects like two user objects.
0:10
S…
Speaker 2 (8- Comparable Interface)
Now,
0:11
S…
Speaker 1 (8- Comparable Interface)
what is the benefit of this?
0:12
S…
Speaker 2 (8- Comparable Interface)
Well,
0:12
S…
Speaker 1 (8- Comparable Interface)
a lot of sorting algorithms are based on comparing objects.
0:16
S…
Speaker 1 (8- Comparable Interface)
So with these comparisons,
0:18
S…
Speaker 1 (8- Comparable Interface)
we can determine what objects should come first.
0:20
S…
Speaker 1 (8- Comparable Interface)
I've talked about this in detail in my ultimate data structures and algorithms
0:25
S…
Speaker 1 (8- Comparable Interface)
course, which is the course that I recommend you to watch after finishing the advanced Java
0:29
S…
Speaker 1 (8- Comparable Interface)
course.
0:30
S…
Speaker 1 (8- Comparable Interface)
Now let's see how we can use the comparable interface for comparing objects.
0:33
S…
Speaker 1 (8- Comparable Interface)
So here on Java documentation,
0:36
S…
Speaker 1 (8- Comparable Interface)
you can see that comparable is a generic interface,
0:39
S…
Speaker 1 (8- Comparable Interface)
because here we have a type parameter.
0:41
S…
Speaker 1 (8- Comparable Interface)
And if you scroll down,
0:43
S…
Speaker 1 (8- Comparable Interface)
you can see that this interface has a single method called compareTo,
0:48
S…
Speaker 1 (8- Comparable Interface)
which takes an object.
0:49
S…
Speaker 1 (8- Comparable Interface)
Now the type of this object is t,
0:51
S…
Speaker 1 (8- Comparable Interface)
which is the type parameter for this generic interface.
0:54
S…
Speaker 1 (8- Comparable Interface)
So to be able to compare two user objects,
0:57
S…
Speaker 1 (8- Comparable Interface)
we should implement this interface in our user class.
0:59
S…
Speaker 1 (8- Comparable Interface)
So here in the user class we type implements comparable
1:04
S…
Speaker 1 (8- Comparable Interface)
of user.
1:06
S…
Speaker 1 (8- Comparable Interface)
Now we have a compilation error because we haven't implemented this interface yet.
1:10
S…
Speaker 1 (8- Comparable Interface)
So we press alt and enter and ask IntelliJ to
1:14
S…
Speaker 1 (8- Comparable Interface)
implement the methods of this interface.
1:18
S…
Speaker 1 (8- Comparable Interface)
IntelliJ is telling us that the comparable interface has a single method,
1:22
S…
Speaker 1 (8- Comparable Interface)
which is highlighted,
1:23
S…
Speaker 1 (8- Comparable Interface)
which means we're going to implement this.
1:25
S…
Speaker 2 (8- Comparable Interface)
So,
1:25
S…
Speaker 2 (8- Comparable Interface)
okay,
1:26
S…
Speaker 1 (8- Comparable Interface)
and here's our comparative method.
1:29
S…
Speaker 1 (8- Comparable Interface)
Now look at the type of parameter of this method.
1:32
S…
Speaker 1 (8- Comparable Interface)
It's user,
1:33
S…
Speaker 1 (8- Comparable Interface)
because we passed the user class over here.
1:35
S…
Speaker 1 (8- Comparable Interface)
Now let me show you what happens if you forget to pass this generic
1:39
S…
Speaker 1 (8- Comparable Interface)
type argument.
1:39
S…
Speaker 1 (8- Comparable Interface)
So,
1:40
S…
Speaker 1 (8- Comparable Interface)
I'm going to undo the changes.
1:43
S…
Speaker 1 (8- Comparable Interface)
and simply type implements comparable without
1:47
S…
Speaker 1 (8- Comparable Interface)
the generic type argument.
1:49
S…
Speaker 1 (8- Comparable Interface)
Okay, now let's implement the methods.
1:52
S…
Speaker 1 (8- Comparable Interface)
Look at the type of this parameter.
1:55
S…
Speaker 1 (8- Comparable Interface)
It's object.
1:56
S…
Speaker 1 (8- Comparable Interface)
So instead of comparing a user object with another user object,
2:01
S…
Speaker 1 (8- Comparable Interface)
now we are comparing this with an instance of the object class.
2:05
S…
Speaker 1 (8- Comparable Interface)
Earlier I told you the problems with the object class.
2:07
S…
Speaker 1 (8- Comparable Interface)
We have to do an explicit cast here and it is possible to get a casting exception
2:11
S…
Speaker 1 (8- Comparable Interface)
at runtime.
2:12
S…
Speaker 1 (8- Comparable Interface)
So this approach is unsafe and that's why we should always
2:16
S…
Speaker 1 (8- Comparable Interface)
specify the generic type argument.
2:20
S…
Speaker 1 (8- Comparable Interface)
So here we pass the user class as the generic type argument.
2:23
S…
Speaker 2 (8- Comparable Interface)
Now,
2:26
S…
Speaker 1 (8- Comparable Interface)
here's the logic we should implement here.
2:28
S…
Speaker 1 (8- Comparable Interface)
If the current user object is less than this
2:32
S…
Speaker 1 (8- Comparable Interface)
other user object,
2:33
S…
Speaker 1 (8- Comparable Interface)
that is this parameter here,
2:35
S…
Speaker 1 (8- Comparable Interface)
then we should return a negative value.
2:37
S…
Speaker 1 (8- Comparable Interface)
It could be negative one,
2:39
S…
Speaker 1 (8- Comparable Interface)
negative two,
2:39
S…
Speaker 1 (8- Comparable Interface)
whatever, it doesn't really matter.
2:41
S…
Speaker 1 (8- Comparable Interface)
If they're equal,
2:44
S…
Speaker 1 (8- Comparable Interface)
then we should return 0.
2:46
S…
Speaker 1 (8- Comparable Interface)
And if the current user object is greater than the other user object,
2:50
S…
Speaker 1 (8- Comparable Interface)
then we should return a positive value.
2:52
S…
Speaker 1 (8- Comparable Interface)
Again, it could be 1,
2:53
S…
Speaker 1 (8- Comparable Interface)
2, it doesn't matter.
2:54
S…
Speaker 1 (8- Comparable Interface)
So on what basis are we going to compare these two user objects?
2:58
S…
Speaker 1 (8- Comparable Interface)
In other words,
2:59
S…
Speaker 1 (8- Comparable Interface)
how can we say if a user object is greater than another user object
3:03
S…
Speaker 1 (8- Comparable Interface)
or not?
3:04
S…
Speaker 2 (8- Comparable Interface)
Well,
3:05
S…
Speaker 1 (8- Comparable Interface)
let's assume that we're going to compare users by their points.
3:08
S…
Speaker 1 (8- Comparable Interface)
Later,
3:09
S…
Speaker 1 (8- Comparable Interface)
we'll be able to sort users by their points.
3:11
S…
Speaker 1 (8- Comparable Interface)
We'll look at that in the next section.
3:13
S…
Speaker 1 (8- Comparable Interface)
So let's declare a private field of type
3:17
S…
Speaker 1 (8- Comparable Interface)
integer called points.
3:19
S…
Speaker 1 (8- Comparable Interface)
Now we want to initialize this from the constructor.
3:21
S…
Speaker 1 (8- Comparable Interface)
So let's quickly add a constructor public user integer
3:26
S…
Speaker 1 (8- Comparable Interface)
points, and here we set this .points equals
3:30
S…
Speaker 1 (8- Comparable Interface)
points.
3:31
S…
Speaker 1 (8- Comparable Interface)
Now back to our compareTo method.
3:34
S…
Speaker 1 (8- Comparable Interface)
Here's one way to implement this logic.
3:37
S…
Speaker 1 (8- Comparable Interface)
If points is less than o
3:42
S…
Speaker 1 (8- Comparable Interface)
.points, then we return negative 1.
3:46
S…
Speaker 1 (8- Comparable Interface)
If they're equal,
3:47
S…
Speaker 1 (8- Comparable Interface)
then we return 0 and otherwise
3:51
S…
Speaker 1 (8- Comparable Interface)
we return negative 1.
3:54
S…
Speaker 1 (8- Comparable Interface)
But there is a better way.
3:56
S…
Speaker 1 (8- Comparable Interface)
We can simply return points minus
4:00
S…
Speaker 1 (8- Comparable Interface)
O dot points.
4:01
S…
Speaker 1 (8- Comparable Interface)
So if this user has more points than the other user,
4:05
S…
Speaker 1 (8- Comparable Interface)
the result of this expression is going to be a positive number.
4:08
S…
Speaker 1 (8- Comparable Interface)
If they're equal,
4:10
S…
Speaker 1 (8- Comparable Interface)
it's going to be 0.
4:11
S…
Speaker 1 (8- Comparable Interface)
And if this user has fewer points than the other user,
4:14
S…
Speaker 1 (8- Comparable Interface)
the result of this expression is going to be a negative number.
4:17
S…
Speaker 1 (8- Comparable Interface)
So we don't really need these few lines over here.
4:20
S…
Speaker 2 (8- Comparable Interface)
Okay?
4:21
S…
Speaker 1 (8- Comparable Interface)
Now also for clarity,
4:23
S…
Speaker 1 (8- Comparable Interface)
I would prefer to rename this argument to other.
4:27
S…
Speaker 1 (8- Comparable Interface)
That's better.
4:28
S…
Speaker 1 (8- Comparable Interface)
So this is how we can implement the
4:32
S…
Speaker 1 (8- Comparable Interface)
comparable interface.
4:33
S…
Speaker 1 (8- Comparable Interface)
Now back in the main class,
4:35
S…
Speaker 1 (8- Comparable Interface)
let's compare two user objects.
4:36
S…
Speaker 1 (8- Comparable Interface)
So here's one user,
4:38
S…
Speaker 1 (8- Comparable Interface)
new user with 10 points.
4:41
S…
Speaker 1 (8- Comparable Interface)
And here's another user with 20 points.
4:45
S…
Speaker 1 (8- Comparable Interface)
Now in Java we cannot write code like this if user1 is
4:49
S…
Speaker 1 (8- Comparable Interface)
less than user2.
4:51
S…
Speaker 1 (8- Comparable Interface)
We can only use this operator to compare numbers and characters.
4:55
S…
Speaker 1 (8- Comparable Interface)
So to compare two user objects,
4:57
S…
Speaker 1 (8- Comparable Interface)
we'll have to use our compareTo method.
5:00
S…
Speaker 1 (8- Comparable Interface)
We pass user2 here,
5:02
S…
Speaker 1 (8- Comparable Interface)
then we check to see if the result is less than 0.
5:06
S…
Speaker 1 (8- Comparable Interface)
We can print a message like user1 is
5:10
S…
Speaker 1 (8- Comparable Interface)
smaller than user2.
5:13
S…
Speaker 1 (8- Comparable Interface)
else if user 1 .compared to user 2 equals
5:18
S…
Speaker 1 (8- Comparable Interface)
0, then we can print user 1 equals
5:22
S…
Speaker 1 (8- Comparable Interface)
user 2.
5:23
S…
Speaker 2 (8- Comparable Interface)
Otherwise,
5:24
S…
Speaker 1 (8- Comparable Interface)
we're going to print user 1 is greater than user
5:28
S…
Speaker 1 (8- Comparable Interface)
2. Let's run the program.
5:30
S…
Speaker 1 (8- Comparable Interface)
So user 1 is less than user 2
5:34
S…
Speaker 1 (8- Comparable Interface)
because it has fewer points.
This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy
Samenvatting
Klik op Summarize om een AI samenvatting van dit transcript te genereren.
Samengevat...
Vraag AI over dit Transcript
Vraag maar iets over dit transcript De AI zal relevante secties en antwoord vinden.