What are the differences between compareTo() and compare() methods in Java?\\n



The compareTo() and compare() methods are both used to compare two objects. They return an int value after comparison, which tells if both objects are equal, if not, which is lesser, and which is greater.

The primary difference between these two methods is that the compareTo() method is used when the Comparable interface is implemented, whereas the compare() method is used when the Comparator interface is implemented.

Let's understand the use of compareTo() and compare() methods in Java, and then we will discuss how these two methods are different from each other.

The compareTo() Method in Java

The compareTo() method compares the current (this) object with the specified object and returns an integer. The returned integer will be -ve number if & only if this object is less than the specified object. It will be +ve number if & only if this object is greater than 1. And, it will be 0 if both objects are equal.

The compareTo() method is defined in Comparable interface that belongs to java.lang package.

Example: Use of compareTo() Method

A Java program that demonstrates the use of compareTo() method is given below:

import java.util.*;
class Employee implements Comparable<Employee> {
   String name;
   int age;
   Employee(String name, int age) {
      this.name = name;
      this.age = age;
   }
   //overridden compareTo method
   @Override
   public int compareTo(Employee o) {
      return this.age - o.age;
   }
}
public class ComparableDemo {
   public static void main(String[] args) {
      // CREATION
      List<Employee> list = new ArrayList<>();
      //INSERTION
      list.add(new Employee("Krishna", 30));
      list.add(new Employee("Archana", 28));
      list.add(new Employee("Vineet", 25));
      list.add(new Employee("Ramesh", 38));
      list.add(new Employee("Alok", 28));
      System.out.println("Before sorting: ");
      for (Employee e : list) {
         System.out.print("[ EMP : age = " + e.age + " ] ");
      }
      //SORTING
      Collections.sort(list);
      System.out.println("After sorting: ");
      for (Employee e : list) {
         System.out.print("[ EMP : age = " + e.age + " ] ");
      }
   }
}

Output of the above code is as given below:

Before sorting: 
[ EMP : age = 30 ] [ EMP : age = 28 ] [ EMP : age = 25 ] [ EMP : age = 38 ] [ EMP : age = 28 ] 
After sorting: 
[ EMP : age = 25 ] [ EMP : age = 28 ] [ EMP : age = 28 ] [ EMP : age = 30 ] [ EMP : age = 38 ] 

The compare() Method

The compare() method compares the first object with the second object and returns an integer. These objects are passed as arguments to the method. The Comparator interface provides this method for performing sorting operations.

The compare() method returns -ve number if the first object is less than the second one, +ve number if the first one is greater than the second object and 0 if both are equal.

Example: Use of compare() Method

The following example shows the use of compare() method in a Java program:

import java.util.*;
class Student {
   String name;
   int age, roll;
   Student(String name, int age, int roll) {
      this.name = name;
      this.age = age;
      this.roll = roll;
   }
}
class AgeComparator implements Comparator<Student> {
   @Override
   public int compare(Student o1, Student o2) {
      return o1.age - o2.age;
   }
}
class RollComparator implements Comparator<Student> {
   @Override
   public int compare(Student o1, Student o2) {
      return o1.roll - o2.roll;
   }
}
public class ComparatorDemo {
   public static void main(String[] args) {
      List<Student> list = new ArrayList<>();
      list.add(new Student("Ramesh", 30, 20));
      list.add(new Student("Adithya", 7, 10));
      list.add(new Student("Krishna", 25, 5));
      list.add(new Student("Vineet", 24, 15));
      System.out.println("BEFORE SORTING");
      for (Student e : list) {
         System.out.println("[ STU : name = " + e.name + " age = " + e.age + " roll = " + e.roll + "]");
      }
      Collections.sort(list,new AgeComparator());
      System.out.println("AFTER SORTING WITH AGE");
      for (Student e : list) {
         System.out.println("[ STU : name = " + e.name + " age = " + e.age + " ]");
      }
      Collections.sort(list,new RollComparator());
      System.out.println("AFTER SORTING WITH ROLL");
      for (Student e : list) {
         System.out.println("[ STU : name = " + e.name + " roll = " + e.roll + " ]");
      }
   }
}

On running, the above Java program will display the below output:

BEFORE SORTING
[ STU : name = Ramesh age = 30 roll = 20]
[ STU : name = Adithya age = 7 roll = 10]
[ STU : name = Krishna age = 25 roll = 5]
[ STU : name = Vineet age = 24 roll = 15]
AFTER SORTING WITH AGE
[ STU : name = Adithya age = 7 ]
[ STU : name = Vineet age = 24 ]
[ STU : name = Krishna age = 25 ]
[ STU : name = Ramesh age = 30 ]
AFTER SORTING WITH ROLL
[ STU : name = Krishna roll = 5 ]
[ STU : name = Adithya roll = 10 ]
[ STU : name = Vineet roll = 15 ]
[ STU : name = Ramesh roll = 20 ]

compareTo() method VS compare() method in Java

The following table shows how compareTo() is different from compare() method:

compareTo() method

compare() method

The Comparable interface provides compareTo() method.

The compare() method is defined in Comparator interface.

It i used to define the natural ordering of objects.

It defines custom sorting logic.

It compares the current object (this) with another specified object.

It compares two given objects passed as arguments.

Updated on: 2025-05-20T19:28:58+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements