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.
- //Java Program to demonstrate the use of Java Comparable.
- //Creating a class which implements Comparable Interface
- import java.util.*;
- import java.io.*;
- class Student implements Comparable<Student>{
- int rollno;
- String name;
- int age;
- Student(int rollno,String name,int age){
- this.rollno=rollno;
- this.name=name;
- this.age=age;
- }
- public int compareTo(Student st){
- if(age==st.age)
- return 0;
- else if(age>st.age)
- return 1;
- else
- return -1;
- }
- }
- //Creating a test class to sort the elements
- public class Main {
- public static void main(String args[]){
- ArrayList<Student> al=new ArrayList<Student>();
- al.add(new Student(101,"Peter",23));
- al.add(new Student(106,"Andrew",27));
- al.add(new Student(105,"John",21));
- Collections.sort(al);
- for(Student st:al){
- System.out.println(st.rollno+" "+st.name+" "+st.age);
- }
- }
- }
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
- class Student{
- int rollno;
- String name;
- int age;
- Student(int rollno,String name,int age){
- this.rollno=rollno;
- this.name=name;
- this.age=age;
- }
- }
AgeComparator.java
- import java.util.*;
- class AgeComparator implements Comparator<student>{
- public int compare(Student s1,Student s2){
- if(s1.age==s2.age)
- return 0;
- else if(s1.age>s2.age)
- return 1;
- else
- return -1;
- }
- }
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
- import java.util.*;
- class NameComparator implements Comparator<student>{
- public int compare(Student s1,Student s2){
- return s1.name.compareTo(s2.name);
- }
- }
In this class, we are printing the values of the object by sorting on the basis of name and age.
- //Java Program to demonstrate the use of Java Comparator
- import java.util.*;
- import java.io.*;
- public class Main{
- public static void main(String args[]){
- //Creating a list of students
- ArrayList<student> al=new ArrayList<student>();
- al.add(new Student(101,"Peter",23));
- al.add(new Student(106,"Andrew",27));
- al.add(new Student(105,"Jack",21));
- System.out.println("Sorting by Name");
- //Using NameComparator to sort the elements
- Collections.sort(al,new NameComparator());
- //Traversing the elements of list
- for(Student st: al){
- System.out.println(st.rollno+" "+st.name+" "+st.age);
- }
- System.out.println("sorting by Age");
- //Using AgeComparator to sort the elements
- Collections.sort(al,new AgeComparator());
- //Travering the list again
- for(Student st: al){
- System.out.println(st.rollno+" "+st.name+" "+st.age);
- }
- }
- }
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
Post a Comment