Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Comparator and Comparable Interface in Java

In the previous video we have seen that what is Java Swing with example, and also about Event Handling in Java with types of Events in Java. Now in this video we are going to see that what are Comparator Interface and Comparable Interface in Java programming language.

Comparator Interface in Java is used to order the object in our own way. It gives you the ability to decide how elements are stored within sorted collection and map.

Comparator interface in Java defines compare() method.
This method compare two object and return 0 if two object are equal. It returns a positive value if object1 is greater than object2. Otherwise a negative value is return. The method can throw a Class CastException if the type of object are not compatible for comparison.

Now we will see an Example for Comparator interface in Java,
import java.util.*;
class Dog Implements Comparator,Comparable
private String name;
private int age;
Dog()
Dog(String n, int a)
{
name=n;
age=a;
}
public String getDogName()
{
return name;
}
public int getDogAge()
{
return age;
}
//Overriding the compareTo method
public int compareTo(Dog d)
{
return (this.name).compareTo(d.name);
}
public int compare(Dog d, Dog d1)
{
return d.age - d1.age;
}
}
public class Example
{
public static void main (String args[])
{
//Takes a list of Dog objects
List list = new ArrayList();
list.add(new Dog("Shaggy",3));
list.add(new Dog("Lacy",2));
list.add(new Dog("Roger",10));
list.add(new Dog ("Tommy",4));
list.add(new Dog ("Tanmy",1));
Collections.sort (list);
//Sorts the array list
for(Dog a: list)
//printing the sorted list of names System.out.print(a.getDogName() + ", ");
//Sorts the array list using comparator Collections.sort(list, new Dog ());
System.out.println(" ");
for(Dog a: list) //printing the sorted list of ages System.out.print(a.getTagName() + " :" +
a.getDogAge() + ", ");
}
}
Output :
Lacy, Roger, Shaggy, Tanmy, Tommy,
Tanmy :1, Lacy :2, Shaggy :3, Tommy :4, Roger :10,

Comparable Interface in Java programming is implemented by a class in order to be able to comparing object of itself with some other objects. The class itself must implement the interface in order to be able to compare its instance(s).

The method required for Comparable Interface Implementation is compareTo().

Now we will see an Example for Comparable Interface in Java.
class HDTV implements Comparable {
private int size;
private String brand;
public HDTV (int size, String brand)
{
this.size = size;
this.brand = brand;
public int getSize ()
{
return size;
}
public void setSize(int size)
{
this.size = size;
}
public String getBrand() return brand;
public void setBrand (String brand)
{
this.brand = brand;
}
//Override
public int compareTo (HDTV tv)
{
if (this.getSize() > tv.getSize ())
return 1;
else if (this.getSize() < tv.getSize())
return -1;
else return 0;
public class Main {
public static void main (String() args)
{
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
if (tv1.compareTo(tv2) > 0)
{
System.out.println(tv1.getBrand() + " is better.");
}
else
{
System.out.println(tv2.get Brand () + " is better.");
} }
}
Output :
Sony is better

Thus, this is how Comparator Interface and Comparable Interface in Java programming works. Now in the next video, we will see Java Swing and features of Java Swing with easy-to-understand example.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere