What is the difference between Comparable and Comparator ?
You may say Comparable is present in java.lang package and Comparator in java.util package. Former has compareTo(Obect o) method and later has compare(Object o1, Object o2) method.
This is all passe….
People want to hear the real difference. To understand the real difference lets dive into the code, lets see some example.
How to sort an ArrayList object containing Employee objects ?
Lets do it by creating an Employee class:
class Employee implements Comparable{ int age; public Employee(int age) { this.age=age; } public int compareTo(Object o) { Employee e=(Employee)o; return this.age-e.age; } public String toString() { return String.valueOf(age); } } public class Manager{ public static void main(String[] args) { Employee e1=new Employee(41); Employee e2=new Employee(35); Employee e3=new Employee(10); List list=new ArrayList(); list.add(e1); list.add(e2); list.add(e3); System.out.println("Before sort:"+ list); Collections.sort(list); System.out.println("After sort:" + list); } }
Now lets do the above activity using Comparator:
class Employee implements Comparator{ int age; public Employee() { } public Employee(int age) { this.age=age; } public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; return e1.age - e2.age; } public String toString() { return String.valueOf(age); } } public class Manager{ public static void main(String[] args) { Employee e1=new Employee(41); Employee e2=new Employee(35); Employee e3=new Employee(10); List list=new ArrayList(); list.add(e1); list.add(e2); list.add(e3); System.out.println("Before sort:"+ list); Collections.sort(list, new Employee()); //Specify the Comparator object. You can pass e1 as well. System.out.println("After sort:" + list); } }
Note the difference in both the ways:
Collections.sort(list); Collections.sort(list, new Employee(5));
Is this the only difference between these two ?
No, there are many differences between Comparable and Comparator interface, below are the same:
Comparable | Comparator |
---|---|
1) Comparable provides a single sorting sequence. In other words, we can sort the collection on the basis of a single element such as id, name, and price. | The Comparator provides multiple sorting sequences. In other words, we can sort the collection on the basis of multiple elements such as age, name, and price etc. |
2) Comparable affects the original class, i.e., the actual class is modified. | Comparator doesn’t affect the original class, i.e., the actual class is not modified. |
3) Comparable provides compareTo() method to sort elements. | Comparator provides compare() method to sort elements. |
4) Comparable is present in java.lang package. | A Comparator is present in the java.util package. |
5) We can sort the list elements of Comparable type by Collections.sort(List) method. | We can sort the list elements of Comparator type by Collections.sort(List, Comparator) method. |
To prove the point no. 1 of the above difference table below is the code :
How to sort an object based on multiple parameter ?
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; class Employee{ int age; String name; public Employee(int age, String name) { this.age=age; this.name=name; } public String toString() { return String.valueOf(age) + "-" +name; } } class EmployeeAgeComparator implements Comparator{ public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; return e1.age - e2.age; } } class EmployeeNameComparator implements Comparator{ public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; return e1.name.compareTo(e2.name); } } public class Manager{ public static void main(String[] args) { Employee e1=new Employee(41,"Prasanna"); Employee e2=new Employee(35,"Ashima"); Employee e3=new Employee(10,"Baby"); List list=new ArrayList(); list.add(e1); list.add(e2); list.add(e3); System.out.println("Before sort:"+ list); //Collections.sort(list, new EmployeeAgeComparator()); //To sort by Age. Collections.sort(list, new EmployeeNameComparator()); //To sort by Name. System.out.println("After sort:" + list); } }
To prove the point no. 2 of the above difference table below is the code, which is the re-written format of the above code:
class Employee{ int age; public Employee(int age) { this.age=age; } public String toString() { return String.valueOf(age); } } class EmployeeComparator implements Comparator{ public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; return e1.age - e2.age; } } public class Manager{ public static void main(String[] args) { Employee e1=new Employee(41); Employee e2=new Employee(35); Employee e3=new Employee(10); List list=new ArrayList(); list.add(e1); list.add(e2); list.add(e3); System.out.println("Before sort:"+ list); Collections.sort(list, new EmployeeComparator()); //Specify the Comparator object. System.out.println("After sort:" + list); } }
Output:
Before sort:[41-Prasanna, 35-Ashima, 10-Baby] After sort:[35-Ashima, 10-Baby, 41-Prasanna]
How to sort an Employee object first on age attribute and then on name attribute ?
Below is the logic you need to keep in the compareTo() or compare():
public int compareTo(Object o) { Employee e=(Employee)o; int ret; ret= this.age - e.age; if(ret==0) { ret=this.name.compareTo(e.name); } return ret; }
Full code:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; class Employee{ int age; String name; public Employee(int age, String name) { this.age=age; this.name=name; } public String toString() { return String.valueOf(age) + "-" +name; } } class EmployeeAgeComparator implements Comparator{ public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; return e1.age - e2.age; } } class EmployeeAgeAndNameComparator implements Comparator{ public int compare(Object o1, Object o2) { Employee e1=(Employee)o1; Employee e2=(Employee)o2; int ret; ret= e1.age - e2.age; if(ret==0) { ret=e1.name.compareTo(e2.name); } return ret; } } public class Manager{ public static void main(String[] args) { Employee e1=new Employee(35,"Akmal"); Employee e2=new Employee(35,"Ashima"); Employee e3=new Employee(10,"Baby"); List list=new ArrayList(); list.add(e1); list.add(e2); list.add(e3); System.out.println("Before sort:"+ list); Collections.sort(list, new EmployeeAgeAndNameComparator()); //To sort by Name. System.out.println("After sort:" + list); } }
Output:
Before sort:[35-Akmal, 35-Ashima, 10-Baby] After sort:[10-Baby, 35-Akmal, 35-Ashima]