Difference Between Comparable and Comparator in Java



Comparable and Comparator both are an interface that can be used to sort the elements of the collection. Comparator interface belongs to java.util package while comparable belongs to java.lang package. Comparator interface sort collection using two objects provided to it, whereas comparable interface compares" this" refers to the one objects provided to it. 

What is a Comparable interface?

The Comparable interface is an interface which is used by Java Collections to sort and compare custom objects. It belongs to java.lang package and has a single method called compareTo().

Example of Comparable

The following is an example of Comparable in java ?

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ComparableExample {
   public static void main(String[] args) {
      List<Laptop> laptopList = new ArrayList<>();
      laptopList.add(new Laptop("HCL", 16, 800));
      laptopList.add(new Laptop("Apple", 8, 100));
      laptopList.add(new Laptop("Dell", 4, 600));
      Collections.sort(laptopList);
      for (Laptop lap : laptopList) {
         System.out.println(lap.getRam());
      }
   }
}

class Laptop implements Comparable<Laptop> {
   String name;
   int ram;
   int price;

   public Laptop(String name, int ram, int price) {
      this.name = name;
      this.ram = ram;
      this.price = price;
   }

   public String getName() {
      return name;
   }

   public int getRam() {
      return ram;
   }

   public int getPrice() {
      return price;
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setRam(int ram) {
      this.ram = ram;
   }

   public void setPrice(int price) {
      this.price = price;
   }

   @Override
   public int compareTo(Laptop o) {
      return Integer.compare(this.ram, o.getRam());
   }
}

The output of the above Java program is ?

4
8
16

What is a Comparator interface?

The Comparator interface defines the order of the objects of user-defined classes. It belongs to java.util package and the two methods of comparator interface are compare() and equals().

Example of Comparator

The following is an example of Comparator in java ?

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Laptop {
    private String name;
    private int ram;
    private int price;

    public Laptop(String name, int ram, int price) {
        this.name = name;
        this.ram = ram;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getRam() {
        return ram;
    }

    public int getPrice() {
        return price;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setRam(int ram) {
        this.ram = ram;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public static void main(String[] args) {
        List<Laptop> laptopList = new ArrayList<>();
        laptopList.add(new Laptop("HCL", 16, 800));
        laptopList.add(new Laptop("Apple", 8, 100));
        laptopList.add(new Laptop("Dell", 4, 600));

        // Sorting using lambda by name
        Comparator<Laptop> nameComparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
        Collections.sort(laptopList, nameComparator);

        for (Laptop lap : laptopList) {
            System.out.println(lap.getName() + " " + lap.getRam() + "GB $" + lap.getPrice());
        }
    }
}

The output of the above Java program is ?

Apple 8GB $100
Dell 4GB $600
HCL 16GB $800

Difference between Comparable and comparator

The following table shows the difference between Comparable and Comparator ?

Sr. No. Key Comparable Comparator
1 Methods
The comparable interface has a method compareTo(Object a ) 
The comparator has a method compare(Object o1, Object O2) 
2
Sorting uses 
Collection.sort(List) method can be used to sort the collection of Comparable type objects.
Collection.sort(List, Comparator) method can be used to sort the collection of Comparator type objects.
 3
Sorting sequence 
Comparable provides single sorting sequence.
The comparator provides a multiple sorting sequence.
4
Package 
Comparable interface belongs to java.lang package.  
Comparator interface belongs to java.util package.
Updated on: 2025-04-15T19:09:18+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements