How compare() method works in Java

Prerequisite: Comparator Interface in Java, TreeSet in Java

The compare() method in Java compares two class specific objects (x, y) given as parameters. It returns the value:

  • 0: if (x==y)
  • -1: if (x < y)
  • 1: if (x > y)

Syntax:

public int compare(Object obj1, Object obj2)

where obj1 and obj2 are the two objects to be compared using compare() method.

Example:

To show working of compare() method using Integer Class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of compare() method using Integer Class
  
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));
    }
}

chevron_right


Output:

-1
0
1

How is the return value evaluated:

The internal working of the compare() method can be visualised with the help of below pseudocode:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Converting the two objects to integer
// for comparison
int intObj1 = (int)obj1;
int intObj2 = (int)obj2;
  
// Get the differnce
int difference = intObj1 - intObj2;
  
if (difference == 0) {
  
    // Both are equal
    return 0;
}
else if (difference < 0) {
  
    // obj1 < obj2
    return -1;
}
else {
  
    // obj1 > obj2
    return 1;
}

chevron_right


Visualising the compare() method with this approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of compare() method
  
import java.lang.Integer;
  
class Gfg {
  
    // Function to compare both objects
    public static int compare(Object obj1, Object obj2)
    {
  
        // Converting the two objects to integer
        // for comparison
        int intObj1 = (int)obj1;
        int intObj2 = (int)obj2;
  
        // Get the differnce
        int difference = intObj1 - intObj2;
  
        if (difference == 0) {
  
            // Both are equal
            return 0;
        }
        else if (difference < 0) {
  
            // obj1 < obj2
            return -1;
        }
        else {
  
            // obj1 > obj2
            return 1;
        }
    }
  
    // 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(compare(a, b));
  
        int x = 30;
        int y = 30;
  
        // as 30 equals 30,
        // Output will be zero
        System.out.println(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(compare(w, z));
    }
}

chevron_right


Output:

-1
0
1

Various possible implemetations of compare() method

public int compare(Object obj1, Object obj2)
{
    Integer I1 = (Integer)obj1; // typecasting object type into integer type
    Integer I2 = (Integer)obj2; // same as above ..
    // 1.
    return I1.compareTo(I2); // ascending order [0, 5, 10, 15, 20]
    // 2.
    return -I1.compareTo(I2); // descending order [20, 15, 10, 5, 0]
    // 3.
    return I2.compareTo(I1); // descending order [20, 15, 10, 5, 0]
    // 4.
    return -I2.compareTo(I1); // ascending order [0, 5, 10, 15, 20]
    // 5.
    return +1; // insertion order [10, 0, 15, 5, 20, 20]
    // 6.
    return -1; // reverse of insertion order [20, 20, 5, 15, 0, 10]
    // 7.
    return 0; // only first element [10]
}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.