Comparable Vs. Comparator in Java

 In Java, Comparable and Comparator both are interfaces used to sort objects.

Comparable

  • It defines natural ordering within a class.
  • It is implemented inside the class being sorted.
  • It uses the compareTo() method.
  • It allows sorting by one one.
  • 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.
  • Comparable affects the original class, i.e., the actual class is modified.
  • Comparable is present in java.lang package.
  • We can sort the list elements of Comparable type by Collections.sort(List) method.
  • Only one compareTo() method can be implemented, restricting flexibility.

Comparator

  • It defines custom sorting logic
  • It is implemented in a separate class or using lambda expressions.
  • It uses the compare() method.
  • It allows sorting by multiple criteria.
  • The Comparator provides multiple sorting sequences. In other words, we can sort the collection on the basis of multiple elements such as id, name, and price etc.
  • Comparator does not affect the original class, i.e., the actual class is not modified.
  • A Comparator is present in the java.util package.
  • We can sort the list elements of Comparator type by Collections.sort(List, Comparator) method.
  • Multiple Comparator classes or lambda expressions can be written to achieve different sorting strategies.

Java Comparable Example

Let's see the example of a Comparable interface that sorts the list elements on the basis of age.


  1. //Java Program to demonstrate the use of Java Comparable.    
  2. //Creating a class which implements Comparable Interface    
  3. import java.util.*;    
  4. import java.io.*;    
  5. class Student implements Comparable<Student>{    
  6. int rollno;    
  7. String name;    
  8. int age;    
  9. Student(int rollno,String name,int age){    
  10. this.rollno=rollno;    
  11. this.name=name;    
  12. this.age=age;    
  13. }    
  14. public int compareTo(Student st){    
  15. if(age==st.age)    
  16. return 0;    
  17. else if(age>st.age)    
  18. return 1;    
  19. else    
  20. return -1;    
  21. }    
  22. }    
  23. //Creating a test class to sort the elements    
  24. public class Main {    
  25. public static void main(String args[]){    
  26. ArrayList<Student> al=new ArrayList<Student>();    
  27. al.add(new Student(101,"Peter",23));    
  28. al.add(new Student(106,"Andrew",27));    
  29. al.add(new Student(105,"John",21));    
  30. Collections.sort(al);    
  31. for(Student st:al){    
  32. System.out.println(st.rollno+" "+st.name+" "+st.age);    
  33. }    
  34. }    
  35. }    

Output :-
105 John 21
101 Peter 23
106 Andrew 27

Java Comparator Example

Let's see an example of the Java Comparator interface where we are sorting the elements of a list using different comparators.

Student.java

  1. class Student{    
  2. int rollno;    
  3. String name;    
  4. int age;    
  5. Student(int rollno,String name,int age){    
  6. this.rollno=rollno;    
  7. this.name=name;    
  8. this.age=age;    
  9. }    
  10. }    


AgeComparator.java

  1. import java.util.*;    
  2. class AgeComparator implements Comparator<student>{    
  3. public int compare(Student s1,Student s2){    
  4. if(s1.age==s2.age)    
  5. return 0;    
  6. else if(s1.age>s2.age)    
  7. return 1;    
  8. else    
  9. return -1;    
  10. }    
  11. }    


This class provides comparison logic based on the name. In such case, we are using the compareTo() method of String class, which internally provides the comparison logic.

NameComparator.java


  1. import java.util.*;    
  2. class NameComparator implements Comparator<student>{    
  3. public int compare(Student s1,Student s2){    
  4. return s1.name.compareTo(s2.name);    
  5. }    
  6. }   

In this class, we are printing the values of the object by sorting on the basis of name and age.



  1. //Java Program to demonstrate the use of Java Comparator    
  2. import java.util.*;    
  3. import java.io.*;    
  4. public class Main{    
  5. public static void main(String args[]){    
  6. //Creating a list of students    
  7. ArrayList<student> al=new ArrayList<student>();    
  8. al.add(new Student(101,"Peter",23));    
  9. al.add(new Student(106,"Andrew",27));    
  10. al.add(new Student(105,"Jack",21));    
  11. System.out.println("Sorting by Name");    
  12. //Using NameComparator to sort the elements    
  13. Collections.sort(al,new NameComparator());    
  14. //Traversing the elements of list    
  15. for(Student st: al){    
  16. System.out.println(st.rollno+" "+st.name+" "+st.age);    
  17. }    
  18. System.out.println("sorting by Age");    
  19. //Using AgeComparator to sort the elements    
  20. Collections.sort(al,new AgeComparator());    
  21. //Travering the list again    
  22. for(Student st: al){    
  23. System.out.println(st.rollno+" "+st.name+" "+st.age);    
  24. }    
  25. }    

Output:

Sorting by Name
106 Andrew 27
105 Jack 21
101 Peter 23

Sorting by Age
105 Jack 21
101 Peter 23
106 Andrew 27

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Increasing Triplet Subsequence