Introduction
In Java are the two interfaces, Comparable and Comparator. As their name suggest, they are used for comparison in Java. One is used to compare its own objects with other objects and the other is used to compare various objects in Java.
Comparable in Java
Comparable is an interface in Java. In Java, Comparable compares its own objects with another object. It uses a compareTo( ) method to compare two objects in Java. In Java, it is a component of the collection framework. It is present in the java.lang package in Java.
Example
- package demo;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class Demo implements Comparable < Demo >
- {
- String players;
- Demo(String p)
- {
- this.players = p;
- }
- public String getPlayers()
- {
- return players;
- }
- public int compareTo(Demo o)
- {
- return (this.players).compareTo(o.players);
- }
- public static void main(String[] args)
- {
- List < Demo > al = new ArrayList < Demo > ();
- System.out.println("LIST IN SORTED WAY :");
- al.add(new Demo("SACHIN"));
- al.add(new Demo("RAINA"));
- al.add(new Demo("DHONI"));
- al.add(new Demo("VIRAT"));
- al.add(new Demo("GANGULY"));
- al.add(new Demo("ZAHEER"));
- al.add(new Demo("IRFAN"));
- al.add(new Demo("ANIL"));
- Collections.sort(al);
- for (Demo d: al)
- {
- System.out.println(d.getPlayers());
- }
- }
- }
Output
Comparator in Java
Comparator is also an interface in Java. In Java, the comparator compares two objects. It uses a compare( ) method to compare the two objects in Java. In Java, it is also a component of the collection framework. It is also present in the java.lang package in Java.
Example
- package demo;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- public class Demo implements Comparator<Demo>
- {
- String department;
- int experience;
- Demo()
- {
- }
- Demo(String d, int e)
- {
- this.department = d;
- this.experience = e;
- }
- public String getDepartment()
- {
- return department;
- }
- public int getExperience()
- {
- return experience;
- }
- public int compare(Demo d, Demo d1)
- {
- return d.experience - d1.experience;
- }
- public static void main(String[] args)
- {
- List<Demo> al = new ArrayList<Demo>();
- al.add(new Demo("C.S.", 3));
- al.add(new Demo("E.C.", 5));
- al.add(new Demo("I.T.", 6));
- al.add(new Demo("E.N.", 8));
- al.add(new Demo("CIVIL", 12));
- al.add(new Demo("M.E.", 9));
- al.add(new Demo("E.E.E.", 4));
- al.add(new Demo("B.T.", 2));
- Collections.sort(al, new Demo());
- System.out.println("LIST OF EXPERIENCE IN INCREASING ORDER");
- System.out.println("\tDeptt. Experience\n");
- for (Demo c1 : al)
- System.out.println("\t" + c1.getDepartment() + "\t " + c1.getExperience());
- }
- }
Output
Differences between Comparable and Comparator in Java
- The Comparable interface must be implemented by the class that wants to sort its objects.
- The Comparator interface is not necessarily implemented by the class that wants to sort its objects. It can be implemented by any other class in Java.
- The Comparable interface uses a compareTo( ) method to compare the objects.
- The Comparator interface uses a compare( ) method to compare the objects.
- For comparable, we use java.lang.Comparable.
- For comparator, we use java.lang.Comparator.
Summary
This article explained the Comparable and Comparator interfaces and also their differences in Java.