The Arrays.compare() method in Java, available in the java.util.Arrays class, is used to compare two arrays lexicographically (dictionary order). It compares elements one by one to determine their ordering.
- Compares two arrays element by element in lexicographical order.
- Returns 0 if both arrays are equal.
- Returns a negative value if the first array is smaller, and a positive value if it is larger.
Example of Arrays.compare() method, where we compare two integer arrays with the same elements.
import java.util.Arrays;
public class ArrayCompare {
public static void main(String[] args)
{
// Initialize two integer array with same elements
int[] arr1 = { 1, 2, 3, 4 };
int[] arr2 = { 1, 2, 3, 4 };
// compare arr1 and arr2 using compare() method
// and print the result
System.out.println(
"" + Arrays.compare(arr1, arr2));
}
}
Output
0
Explanation: The desired output is 0, because we are given two integer arrays with the same numbers and size.
Syntax of Arrays.compare() Method
Arrays.compare(arr1,arr2); // arr1 and arr2 are two arrays
Parameters: The compare() method accepts two arrays as parameters, with supported data types like String, Integer, Float, Double, Long, and others.
Return Type: The return type of this method is an integer.
- It returns positive value, if the first array is lexicographically greater.
- It returns negative value, if the first array is lexicographically lesser.
It returns 0,if both arrays are equal.
Exceptions: NullPointerException: This exception is thrown, if an array is null when accessed.
Note: When comparing object arrays, if any element is null, it may throw a NullPointerException. To handle null values safely, use Arrays.compare() with a Comparator like Comparator.nullsFirst() or nullsLast().
What is Lexicographical Comparison
Lexicographical comparison means:
- Elements are compared from left to right
- The first mismatched element decides the result
- If all elements are equal, the array lengths are compared
This behavior is similar to how words are compared in a dictionary.
Examples of Arrays compare() Method with Different Data Types
Example 1: Compare Two Integer Arrays (Equal Arrays)
import java.util.Arrays;
public class CompareExample{
public static void main(String[] args)
{
//Initialized two integer array
int[] arr1 ={6, 7, 8, 11, 18, 8, 2, 5};
int[] arr2 ={3, 5, 9, 13, 28, 6, 8, 9};
//compare both integer array using compare method and finally print result
System.out.println(""+ Arrays.compare(arr1,arr2));
}
}
Output
1
Explanation: The result is positive because at the first mismatched index, the element in arr1 is greater than the corresponding element in arr2.
Example 2: Compare Two Float Arrays (Different Values)
import java.util.Arrays;
public class CompareExample{
public static void main(String[] args)
{
// Initialize two float array with element
float[] arr1={5.12f, 8.3f, 9.17f, 2.5f, 8.8f, 5.17f, 4.2f, 7.37f};
float[] arr2={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("" + Arrays.compare(arr1, arr2));
}
}
Output
-1
Explanation: The result is -1 in output because arr1 is lexicographically less than arr2.