Open In App

Java Arrays compare() Method with Examples

Last Updated : 29 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Arrays compare() method in Java comes under the Arrays class and java.util package. This method compares two arrays lexicographically (Dictionary order). There are two different versions of different overloads for Boolean, byte, char, double, float, int, long, short, and Object arrays. This method returns values as per the below-mentioned cases. 

  • It returns 0 if the array is equal to the other array.
  • It returns a value less than 0 is returned if the array is lexicographically  less than the other array in
  • It returns a value greater than 0 if the array is lexicographically greater than the other array (more characters).

A null array is lexicographically less than a non-null array, and the two arrays are considered equal if both are null so that it will print 0 in this case.

Syntax:

Arrays.compare(array1,array2);
// array1 and array2 are two arrays

Parameters and Return Type: The method compare() accepts an array as parameters with different data types example: string, integer, float, double, long, etc. The return type of this method is an integer. It returns a positive value if the array is lexicographically greater, negative if it is lesser, and 0 if equal. 

Exceptions: It usually throws NullPointerException and ClassCastException, and both of these exceptions have different means too.

  • NullPointerException: The NullPointerException is a runtime exception that refers to null and occurs when a variable is accessed that does not point to any object.
  • ClassCastException: This exception occurs when we’re trying to convert one class object into another class-type object.

If we want to invoke this method, First import the “Arrays” class from JavaJava. util packages just by adding the “import java.util.Arrays” command. then invoke method by “Arrays” follow “.compare(parameter1,parameter2);” by this you can invoke compare() method easily.  

Example 1: Let’s take an example, First import java.util.Arrays, create a public class named CompareExample, then Initialize two integer arrays with elements, compare them using the compare() method, and finally print the result from the compare method. 

Java




import java.util.Arrays;
 
public class CompareExample{
    public static void main(String[] args)
    {
        //Initialized two integer array
        int[] array1 ={6, 7, 8, 11, 18, 8, 2, 5};       
        int[] array2 ={3, 5, 9, 13, 28, 6, 8, 9};
        //compare both integer array using compare method and finally print result      
        System.out.println("Result is "+ Arrays.compare(array1,array2));
 
    }
}


Output

Result is 1

Hence the desired output is 1 because array1 is lexicographically greater than array2.

Example 2: Let’s take another example, In this example, we’ll take an array of different data types, and that is float, and do the same as we did in the example as mentioned above, first import class, create a new class named “CompareExample” then initialize two floating type array with elements then compare them using compare method of array class and finally print the result we got.

Java




// import Arrays class from java.util package
import java.util.Arrays;
 
public class CompareExample{
   
    public static void main(String[] args)
    {
        // Initialize two float array with element
        float[] floatArray1={5.12f, 8.3f, 9.17f, 2.5f, 8.8f, 5.17f, 4.2f, 7.37f};
        float[] floatArray2={7.12f, 9.3f, 6.17f, 7.5f, 5.8f, 7.17f, 3.2f, 6.37f};
       
        // compare two float array using compare method and finally print result
        System.out.println("Result is " + Arrays.compare(floatArray1, floatArray2));
 
    }
}


Output

Result is -1

The result is -1 in output because floatArray1 is lexicographically less than floatArray2.

Example 3: Let’s take an example. In this, we’ll initialize an array with the same numbers and size. Then compare array1 and array2 and finally prints the result we got from compare method. 

Java




import java.util.Arrays;
 
public class CompareExample {
 
    public static void main(String[] args)
    {
        // Initialize two integer array with same elements
        int[] array1 = { 1, 2, 3, 4 };
        int[] array2 = { 1, 2, 3, 4 };
       
        // compare array1 and array2 using compare() method
        // and print the result
        System.out.println(
            "Result is " + Arrays.compare(array1, array2));
    }
}


Output

Result is 0

The desired output is 0 because we are given two integer arrays with the same numbers and size.

In this article, we’ve learned how to use the compare() method. If array1 is lexicographically greater than array2, it will return a positive(-ve) value. If array1 is lexicographically less than array2, it will return a negative(-ve) value. If both the array is lexicographically equal or null, then it will return 0.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads