How compare() method works in Java

Last Updated : 17 Aug, 2026

The compare() method is used to compare two objects and determine their relative ordering. It is defined in the Comparator interface and is commonly used with collections such as TreeSet, TreeMap, and sorting methods.

The method returns:

  • A negative value if the first object is less than the second object.
  • 0 if both objects are considered equal for comparison.
  • A positive value if the first object is greater than the second object.

Note: The Comparator.compare() contract does not require the method to return exactly -1, 0, or 1. Any negative, zero, or positive integer is valid.

Java
import java.lang.Integer;

class Gfg {

    // driver code
    public static void main(String args[])
    {
        int a = 10;
        int b = 20;

        // as 10 less than 20,
        // Output will be a value less than zero
        System.out.println(Integer.compare(a, b));

        int x = 30;
        int y = 30;

        // as 30 equals 30,
        // Output will be zero
        System.out.println(Integer.compare(x, y));

        int w = 15;
        int z = 8;

        // as 15 is greater than 8,
        // Output will be a value greater than zero
        System.out.println(Integer.compare(w, z));
    }
}

Output
-1
0
1

Explanation:

  • 10 is less than 20, so the result is negative.
  • 30 is equal to 30, so the result is 0.
  • 15 is greater than 8, so the result is positive.

How compare() Works

The compare() method determines the relative order of two objects based on the comparison logic defined by the programmer.

Java
int result = Integer.compare(obj1, obj2);

if (result < 0) {
    // obj1 is smaller
}
else if (result == 0) {
    // obj1 and obj2 are equal
}
else {
    // obj1 is greater
}

Different Ways to Implement compare()

A comparator can define different ordering rules depending on the requirement.

Java
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {

        Comparator<Integer> ascending =
            (a, b) -> Integer.compare(a, b);

        Comparator<Integer> descending =
            (a, b) -> Integer.compare(b, a);

        System.out.println(ascending.compare(10, 20));
        System.out.println(descending.compare(10, 20));
    }
}

Output
-1
1

compare() with TreeSet

The compare() method is commonly used to define the ordering of elements in a TreeSet.

Java
import java.util.Comparator;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {

        TreeSet<Integer> numbers =
            new TreeSet<>((a, b) -> Integer.compare(b, a));

        numbers.add(10);
        numbers.add(5);
        numbers.add(20);
        numbers.add(15);

        System.out.println(numbers);
    }
}

Output
[20, 15, 10, 5]

Explanation: The comparator compares two numbers in reverse order, so the TreeSet stores the elements in descending order.

Advantages of compare()

  • Allows custom ordering of objects.
  • Supports ascending and descending order.
  • Useful for sorting collections.
  • Commonly used with TreeSet and TreeMap.
  • Helps compare user-defined objects.
  • Supports different sorting rules without modifying the original class.
Comment