Signup/Sign In

Java Arrays compare() Method

In this tutorial, we will learn about compare() method from Arrays class with some excellent examples. Before that let's think about why we cannot compare an array directly with == operator? Since the array name has a difference in reference this comparison always will return a false value. To overcome this problem Java Arrays class has compare() method which will check which array is lexicographically smaller or greater.

Syntax

Syntax of the function is given below, it takes two arrays as arguments and returns an integer value based on the comparison of each element of the array.

public static int compare(datatype[] array1,datatype[] array2)

Source Code of this method: This method internally works like the code given below.

int i = Arrays.mismatch(array1, array2);
     if (i >= 0 && i < Math.min(array1.length, array2.length))
         return Boolean.compare(array1[i], array2[i]);
     return array1.length - array2.length;

List of Overloading Methods of compare() Method

This table contains all the overloaded variants of compare() method.

Method Description

static int compare(boolean[] a, boolean[] b)

This method compares two boolean arrays lexicographically.

static int compare(boolean[] a, int aFromIndex,

int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

This method compares two boolean arrays lexicographically over the specified ranges.

static int compare(byte[] a, byte[] b)

This method compares two byte arrays lexicographically.

static int compare(byte[] a, int aFromIndex,

int aToIndex, byte[] b, int bFromIndex, int bToIndex)

This method compares two byte arrays lexicographically over the specified ranges.

static int compare(char[] a, char[] b)

This method compares two char arrays lexicographically.

static int compare(char[] a, int aFromIndex,

int aToIndex, char[] b, int bFromIndex, int bToIndex)

This method compares two char arrays lexicographically over the specified ranges.

static int compare(double[] a, double[] b)

This method compares two double arrays lexicographically.

static int compare(double[] a, int aFromIndex,

int aToIndex, double[] b, int bFromIndex, int bToIndex)

This method compares two double arrays lexicographically over the specified ranges.

static int compare(float[] a, float[] b)

This method compares two float arrays lexicographically.

static int compare(float[] a, int aFromIndex,

int aToIndex, float[] b, int bFromIndex, int bToIndex)

This method compares two float arrays lexicographically over the specified ranges.

static int compare(int[] a, int[] b)

This method compares two int arrays lexicographically.

static int compare(int[] a, int aFromIndex,

int aToIndex, int[] b, int bFromIndex, int bToIndex)

This method compares two int arrays lexicographically over the specified ranges.

static int compare(long[] a, int aFromIndex,

int aToIndex, long[] b, int bFromIndex, int bToIndex)

This method compares two long arrays lexicographically over the specified ranges.

static int compare(long[] a, long[] b)

This method compares two long arrays lexicographically.

static int compare(short[] a, int aFromIndex,

int aToIndex, short[] b, int bFromIndex, int bToIndex)

This method compares two short arrays lexicographically over the specified ranges.

static int compare(short[] a, short[] b)

This method compares two short arrays lexicographically.

static <T extends Comparable<? super T>> int compare(T[] a,

int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)

This method compares two Object arrays lexicographically over the specified ranges.

static <T> int compare(T[] a, int aFromIndex,

int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

This method compares two Object arrays lexicographically over the specified ranges.

static <T extends Comparable<? super T>> int compare(T[] a, T[] b)

This method compares two Object arrays, within comparable elements, lexicographically.

static <T> int compare(T[] a, T[] b, Comparator<? super T> cmp)

This method compares two Object arrays lexicographically using a specified comparator.

Now, let's discuss some examples to note the observation based on the difference in the element of an array.

Example: If both arrays lexicographically equal

If array1 and array2 are equal then compare() returns zero. You can see in the given case both arrays are the same that's why we are getting output as a zero.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array1[]={19,27,55,80}; 
		int array2[]={19,27,55,80}; 
		System.out.println(Arrays.compare(array1,array2));
	}
}


0

Example: If the first array is lexicographically smaller than the Second

If the array1 is lexicographically less than the array2, returns a negative value. In the given example, the value of the first element of the first array is less than the second array and all other elements are the same so they don't count in comparison. So based on the comparison, we are getting -1 as an output.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array1[]={18,5,13,2}; 
		int array2[]={19,5,13,2}; 
		System.out.println(Arrays.compare(array1,array2));
	}
}


-1

Example: If the Second array is lexicographically smaller than the First

If the array1 is lexicographically greater than the array2 returns a positive value. In the code given below, the first element of the array is greater than an element of a second array. So, we are getting 1 as an output.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array1[]={21,15,3,12}; 
		int array2[]={19,15,3,12}; 
		System.out.println(Arrays.compare(array1,array2));
	}
}


1

In a case, we do not want to compare an entire array, there is also another way i.e. slice an array and pass it to compare() function. Syntax for the same is given below:

public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex, short[] b, int FromIndex, int bToIndex)

Example: the compare() Method with slicing

The example below is the same as the examples given above but the only difference is here, we limited comparison in the range by providing startIndex and endIndex for both the arrays. It is an overloaded method of compare() method.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int array1[]={19,15,3,12,16,48,14,7,88,54}; 
		int array2[]={19,15,3,12,16,48,14,7,88,54}; 
		System.out.println(Arrays.compare(array1,2,5,array2,2,5));
	}
}


0

Example: The compare() method and it's all overloading methods

The example is given below illustrates all overloading methods of compare() method. We implemented all the methods using different data types like boolean, byte, char, double, float, int, long and short. We can also see how to implement the compare() method with the corresponding different ranges.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static int	compare(boolean[] a, boolean[] b)
		boolean[] boolArray1={true, false, true, false, true, true};
		boolean[] boolArray2={false, true, false, true, false, true};
		System.out.println("Arrays.compare(boolArray1, boolArray2): "+Arrays.compare(boolArray1, boolArray2));

		//Example static int compare(boolean[] a, int aFromIndex,int aToIndex, boolean[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(boolArray1, 2 , 4, boolArray2, 3, 5): "+Arrays.compare(boolArray1, 2, 4, boolArray2, 3, 5));

		//Example of static int compare(byte[] a, byte[] b)
		byte[] byteArray1={4, 1, 8, 2, 2, 3, 16, 4, 4};
		byte[] byteArray2={7, 9, 4, 12, 9, 7, 2, 14, 8};
		System.out.println("Arrays.compare(byteArray1, byteArray2): "+Arrays.compare(byteArray1, byteArray2));

		//Example static int compare(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(byteArray1, 2, 4, byteArray2, 3, 5): "+Arrays.compare(byteArray1, 2, 4, byteArray2, 3, 5));


		//Example of static int compare(char[] a, char[] b)
		char[] charAarray1={'a', 'e', 'i', 'o', 'u'};
		char[] charAarray2={'a', 'e', 'i', 'o', 'u'};
		System.out.println("Arrays.compare(charAarray1, byteArray2): "+Arrays.compare(charAarray1, charAarray2));

		//Example static int compare(char[] a, int aFromIndex,int aToIndex, char[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(charAarray1, 2, 4, charAarray2, 3, 5): "+Arrays.compare(charAarray1, 2, 4, charAarray2, 3, 5));


		//Example of static int compare(double[] a, double[] b)
		double[] doubleArray1={12, 3, 17, 5, 8, 17, 2, 37};
		double[] doubleArray2={4, 7, 2, 5, 8, 12, 2, 50};
		System.out.println("Arrays.compare(doubleArray1, doubleArray2): "+Arrays.compare(doubleArray1, doubleArray2));

		//Example static int compare(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(doubleArray1, 1, 5, doubleArray2, 3, 5): "+Arrays.compare(doubleArray1, 1, 5, doubleArray2, 3, 5));

		//Example of static int compare(float[] a, float[] b)
		float[] floatArray1={4.12f, 7.3f, 8.17f, 1.5f, 7.8f, 4.17f, 3.2f, 6.37f};
		float[] floatArray2={6.12f, 8.3f, 5.17f, 6.5f, 4.8f, 6.17f, 2.2f, 5.37f};
		System.out.println("Arrays.compare(floatArray1, floatArray2): "+Arrays.compare(floatArray1, floatArray2));

		//Example static int compare(float[] a, int aFromIndex,int aToIndex, float[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(floatArray1, 2, 6, floatArray2, 3, 5): "+Arrays.compare(floatArray1, 2, 6, floatArray2, 3, 5));

		//Example of static int compare(int[] a, int[] b)
		int[] intArray1 ={5, 6, 7, 10, 17, 7, 1, 4};		
		int[] intArray2 ={2, 4, 8, 12, 27, 5, 7, 8};		
		System.out.println("Arrays.compare(intArray1, intArray2): "+Arrays.compare(intArray1, intArray2));

		//Example static int compare(int[] a, int aFromIndex,int aToIndex, int[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(intArray1, 1, 5, intArray2, 0, 4): "+Arrays.compare(intArray1, 1, 5, intArray2, 0, 4));

		//Example of static int compare(long[] a, long[] b)
		long[] longArray1={4,54,56,17,51,84,5,28,33,9};
		long[] longArray2={3,4,16,4,51,12,5,28,8,19};
		System.out.println("Arrays.compare(longArray1, longArray2): "+Arrays.compare(longArray1, longArray2));

		//Example static int compare(long[] a, int aFromIndex,int aToIndex, long[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(longArray1, 0, 4, longArray2, 1, 5): "+Arrays.compare(longArray1, 0, 4, longArray2, 1, 5));
		

		//Example of static int compare(short[] a, short[] b)
		short[] shortArray1={5, 8, 6, 4, 2, 0};
		short[] shortArray2={1, 2, 3, 4, 5, 6};
		System.out.println("Arrays.compare(shortArray1, shortArray2): "+Arrays.compare(shortArray1, shortArray2));

		//Example static int compare(short[] a, int aFromIndex,int aToIndex, short[] b, int bFromIndex, int bToIndex)
		System.out.println("Arrays.compare(shortArray1, 0, 4, shortArray2, 1, 5): "+Arrays.compare(shortArray1, 0, 4, shortArray2, 1, 5));
	}
}


Arrays.compare(boolArray1, boolArray2): 1
Arrays.compare(boolArray1, 2 , 4, boolArray2, 3, 5): 0
Arrays.compare(byteArray1, byteArray2): -3
Arrays.compare(byteArray1, 2, 4, byteArray2, 3, 5): -4
Arrays.compare(charAarray1, byteArray2): 0
Arrays.compare(charAarray1, 2, 4, charAarray2, 3, 5): -6
Arrays.compare(doubleArray1, doubleArray2): 1
Arrays.compare(doubleArray1, 1, 5, doubleArray2, 3, 5): -1
Arrays.compare(floatArray1, floatArray2): -1
Arrays.compare(floatArray1, 2, 6, floatArray2, 3, 5): 1
Arrays.compare(intArray1, intArray2): 1
Arrays.compare(intArray1, 1, 5, intArray2, 0, 4): 1
Arrays.compare(longArray1, longArray2): 1
Arrays.compare(longArray1, 0, 4, longArray2, 1, 5): 1
Arrays.compare(shortArray1, shortArray2): 4
Arrays.compare(shortArray1, 0, 4, shortArray2, 1, 5): 3

Conclusion:

In this tutorial, we learned how to use the compare() function. If the array1 and array2 are equal then compare() returns zero, if the array1 is lexicographically less than the array2 returns a negative value. if the array1 is lexicographically greater than the array2 returns a positive value.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.