Java Essentials - Comparator to sort lists
19K views
Oct 24, 2024
Java Essentials - Comparator to sort lists
View Video Transcript
0:00
In this video we are discussing comparator to short lists
0:05
We know that comparator is one interface and this interface is having mainly two methods
0:11
are there. One is the equals and the one is the compared
0:14
So compared and equals two methods are there. So to use this comparator obviously I must be defining one class which will implement this
0:23
comparator interface and then we can define those methods and you can use them
0:29
So let us go for further discussion on this. So what is comparator
0:35
The comparator is an interface and that is used to compare two objects of a class
0:42
And in this interface there are two methods are there. One is the compared and the one is the equals
0:49
So we need to create a class that can implement this comparator interface
0:54
Then only we can have these methods getting called and getting executed
0:58
it in the collection classes like our add a list or say in case of link list that is a method known as the short method so there is a short method is there in it is available in case of both add a list and in case of link list there is a method known as the short method So there is a short method is there It is available in case of both add a list and in case of link list
1:12
Using this we can short lists. So this short method needs this comparator to compare objects
1:19
for the shorting operation. So now let us go for one practical demonstration for your better
1:25
understanding and there will be discussing all this concept using one Java sample code. So here is the demonstration
1:31
for you we are defining one class that is a string compared which implements the
1:38
interface comparator this comparative interface is defined under the package Java dot util and this particular comparative interface is is having only one
1:49
unimplemented method that is a compared so this the compare is the one
1:54
unimplemented method in this interface that is a comparator and in this
1:59
particular compare we are having two input arguments that is a compare is the T O1 and T O2 that is a type object one and type object two
2:08
Let us go for detail description here You can find that compares it compares it two arguments for the order return a negative integer zero or a positive
2:22
integer as a first argument is less than equal to or greater than the second
2:28
argument and that is the purpose of this compare so compare is the method which we
2:33
have implemented here which returns return STR 1 dot compared to STR 2. Here we have passed this string STR 1 and STR 2 as two objects to this
2:45
compared method. So string compared is nothing but our user defined class which is
2:50
implementing the interface comparator which is defined under the package Java. Util. So here we're having another class that is our comparator demo. Under the
3:02
main function we have defined one add a list object that is a city list and we
3:06
have added multiple string objects to this city list. at a object and we have printed this current content of the city list so let us go
3:13
for the execution you can find that we have printed all the current content of the city list and when the cities are unshorted so Delhi Kolkata Hyderabad Chennai and Lucknow they have got printed now short the ad list in the alphabetical order using the method short and as an input
3:30
argument we are passing the string comparator class object which has already
3:35
implemented the comparative interface and also implemented the method that is a
3:41
compared so city list dot shot new string compared so here we are calling the the string compared class constructor that
3:51
is with that new we are actually passing the string compared compared class
3:56
object as input argument to this short method so now if we go on printing
4:01
this add list content we are getting that this are a list content have got
4:06
shorted in the alphabetical order so now the order has become Chennai Delhi
4:10
Hyderabad Kolkata and Lochnu and you can find that they have got sorted in the alphabetical order
4:15
in the ascending order. In this way in this particular demonstration we have shown
4:20
you that how to use the comparative interface in our Java code. Thanks for watching
4:26
this video
#Java (Programming Language)