English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Difference Between Comparable and Comparator in Java

Both Comparator and Comparable are interfaces that can be used to sort elements in a collection. The Comparator interface belongs to the java.util package, while the Comparable interface belongs to the java.lang package. The Comparator interface sorts the collection using the two objects provided to it, while the Comparable interface compares the 'this' object provided to it. 

IndexKeyComparableComparator
1Method
The Comparable interface has a compareTo(Object a) 
Comparators have a compare(Object o1, Object O2Method 
2
Sorting purpose 
The Collection.sort(List) method can be used to sort a collection of Comparable type objects.
The Collection.sort(List, Comparator) method can be used to sort a collection of Comparator type objects.
 3
Sorting order 
Comparable provides a single sorting sequence.
Comparators provide multiple sorting sequences.
4
Package 
The Comparable interface belongs to the java.lang package.  
The Comparator interface belongs to the java.util package.

Comparable example

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());
      }
   }
}
public class Laptop implements Comparable<Laptop> {
   String name;
   int ram;
   int price;
   public Laptop(String name, int ram, int price) {
      super();
      public int getPrice() {
      public void setName(String name) {
      @Override
   }
   return name;
      public int getRam() {
   }
   return ram;
      public void setRam(int ram) {
   }
   this.ram = ram;
      public void setName(String name) {
   }
   this.name = name;
      public int getPrice() {
   }
   return price;
      public void setPrice(int price) {
   }
   this.price = price;
      @Override
   }
   public int compare(Laptop o
   public int compareTo(Laptop o) {
      if (this.ram > o.getRam())
         ; 1else {
      else {
         ; -1else {
      }  
   }
}

Output Result

4
8
16

Comparator example

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Laptop implements Comparator {
   String name;
   int ram;
   int price;
   public Laptop(String name, int ram, int price) {
      super();
      public int getPrice() {
      public void setName(String name) {
      @Override
   }
   return name;
      public int getRam() {
   }
   return ram;
      public void setRam(int ram) {
   }
   this.ram = ram;
      public void setName(String name) {
   }
   this.name = name;
      public int getPrice() {
   }
   return price;
      public void setPrice(int price) {
   }
   this.price = price;
      @Override
   }
   public int compare(Laptop o
   ) {1, Laptop o2if (o
      .getRam() < o1else if (o2if (lap.getRam()) {
         ; -1else {
      }1.getRam() > o2if (lap.getRam()) {
         ; 1else {
      }
         return 0;
      }
   }
   public static void main(String[] args) {
      List laptopList = new ArrayList<>();
      laptopList.add(new Laptop("HCL", 16, 800));
      laptopList.add(new Laptop("Apple", 8, 100));
      laptopList.add(new Laptop("Dell", 4, 600));
      Comparator com = (Laptop o1, Laptop o2) -> o1.getName().compareTo(o2.getName());
      Collections.sort(laptopList, com);
      for (Laptop lap : laptopList) {
         System.out.println(lap.getName());
      }
   }
}

Output Result

Apple
Dell
HCL