Arrays.compare() in Java: A Comprehensive Guide

In Java, Arrays class provides a compare() method that can be used to compare two arrays lexicographically. The compare() method compares the two arrays, element by element, and returns a value indicating the relationship between them. The compare() method is available in four different versions, depending on the type of arrays and the number of parameters passed to the method.

Syntax of Arrays.compare():

The syntax of the compare() method is as follows:

For primitive type arrays:

  • public static int compare(type[] a, type[] b)

For non-primitive type arrays:

  • public static int compare(Object[] a, Object[] b)
  • public static int compare(type[] a, int aStart, int aEnd, type[] b, int bStart, int bEnd)
  • public static int compare(type[] a, int aStart, int aEnd, type[] b, int bStart, int bEnd, Comparator<? super type> cmp)

Here, type is the type of elements in the array, and cmp is the comparator used to compare the elements in the arrays. The aStart and aEnd parameters define the range of elements to be compared in the first array, and the bStart and bEnd parameters define the range of elements to be compared in the second array.

The compare() method returns the following values:

0 - if the arrays are equal a negative integer - if the first array is lexicographically less than the second array a positive integer - if the first array is lexicographically greater than the second array

Examples of Arrays.compare():

Let's take a look at some examples to understand the working of the compare() method:

Example 1: Comparing two integer arrays

int a[] = {10, 20, 15}; int b[] = {10, 20, 30}; int res = Arrays.compare(a, b); if (res == 0) System.out.println("Arrays are equal"); else if (res > 0) System.out.println("First array is lexicographically greater"); else System.out.println("Second array is lexicographically greater");

Output: Second array is lexicographically greater

Example 2: Comparing sub-arrays of two integer arrays

int a[] = {10, 20, 30, 40}; int b[] = {10, 20, 30}; int res = Arrays.compare(a, 0, 3, b, 0, 3); if (res == 0) System.out.println("Arrays are equal"); else if (res > 0) System.out.println("First sub-array is lexicographically greater"); else System.out.println("Second sub-array is lexicographically greater");

Output: First sub-array is lexicographically greater

Example 3: Comparing two non-primitive type arrays

String a[] = {"apple", "orange", "banana"}; String b[] = {"apple", "orange", "grape"}; int res = Arrays.compare(a, b); if (res == 0) System.out.println("Arrays are equal"); else if (res > 0) System.out.println("First array is lexicographically greater"); else System.out.println("Second array is lexicographically greater");

Output: Second array is lexicographically greater

Example 4: Comparing two non-primitive type arrays with custom Comparator

class LengthComparator implements Comparator<String> { public int compare(String a, String b) { return a.length() - b.length(); } } String a[] = {"apple", "orange", "banana"}; String b[] = {"apple", "orange", "grape"}; int res = Arrays.compare(a, b); System.out.println(res);

The output will be -1, indicating that the first difference between the two arrays was found at the index 2 ("banana" vs "grape").

The Arrays.compare() method compares two arrays lexicographically (i.e., dictionary order), element by element. It returns an integer value indicating the relationship between the two arrays:

  • If the two arrays are equal, it returns 0.
  • If the first array is lexicographically less than the second array, it returns a negative number (usually -1).
  • If the first array is lexicographically greater than the second array, it returns a positive number (usually 1).
Previous Post Next Post