In this quick guide, we will see the newly introduced Java JDK 9 Arrays utility class methods for comparing arrays and examples of comparing arrays.
1. Comparing arrays using Java 9
For comparing arrays and slices of arrays, more static utility methods are added to java.util.Arrays
class in Java 9. Following are the list of overloaded static methods added in Java JDK 9 to the Arrays utility class.
equals()
– returns true if two arrays are equal to each other.compare()
– compares two arrays lexicographically (similar to dictionary order).mismatch()
– finds and returns the index of the first mismatch between two arrays.
1.1. Using equals() method :
equals()
returns true if two arrays are equal to each other. There are two different version of different overloads for boolean
, byte
, char
, double
, float
, int
, long
, short
and Object
arrays. Version one for equality check for two arrays and version two for equality check for slices of two arrays. Two arrays are considered equal if both are null.
Following are overloaded equals()
s
boolean equals(int[] a, int[] b) boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
Example :
public class ComparingArraysEqualsDemo { public static void main(String[] args) { String[] s1 = {"Peter", "Gerhard", "Philip", "Satheesh", "Mike"}; String[] s2 = {"Peter", "Gerhard", "Philip", "Satheesh", "Mike"}; String[] s3 = {"Gerhard", "Mike", "Peter", "Philip", "Satheesh"}; System.out.println("s1: "+ Arrays.toString(s1)); System.out.println("s2: "+ Arrays.toString(s2)); System.out.println("s3: "+ Arrays.toString(s3)); // Comparing arrays for equalitt System.out.println("\nArrays.equals(s1, s2): " + Arrays.equals(s1, s2)); System.out.println("Arrays.equals(s1, s3): " + Arrays.equals(s1, s3)); // Comparing slicces of arrays for equality System.out.println("\nArrays.equals(s1, 0, 3, s3, 0, 3): " + Arrays.equals(s1, 0, 3, s3, 0, 3)); System.out.println("Arrays.equals(s1, 0, 3, s2, 0, 3): " + Arrays.equals(s1, 0, 3, s2, 0, 3)); } }
Output :
s1: [Peter, Gerhard, Philip, Satheesh, Mike] s2: [Peter, Gerhard, Philip, Satheesh, Mike] s3: [Gerhard, Mike, Peter, Philip, Satheesh] Arrays.equals(s1, s2): true Arrays.equals(s1, s3): false Arrays.equals(s1, 0, 3, s3, 0, 3): false Arrays.equals(s1, 0, 3, s2, 0, 3): true
1.2. Using compare() and compareUnsigned() methods
The group of methods
compare() and compareUnsigned() compares two arrays lexicographically. There are two different version of different overloads for boolean
, byte
, char
, double
, float
, int
, long
, short
and Object
arrays. Version one for equality check for two arrays and version two for equality check for slices of two arrays. The compareUnsigned() method treats the integer values as unsigned and this overloaded method applicable only for data types which can be signed.
- A
null
array is lexicographically less than a non-null array. - Two arrays are considered equal if both are
null
. - If first array and second array are equal then
compare()
returns zero; - If the first array (or slice) is lexicographically less than the second array (or slice) returns -ve (negative value).
- If the first array (or slice) is lexicographically greater than the second array (or slice) returns +ve (positive value).
int compare(int[] a, int[] b) int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) int compare(int[] a, int[] b) int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
Example :
public class ComparingArraysCompareDemo { public static void main(String[] args) { int[] i1 = {2, 4, 6, 8, 10}; int[] i2 = {2, 4, 6, 8, 10}; int[] i3 = {2, 4, 12, 8, 10}; System.out.println("i1: "+ Arrays.toString(i1)); System.out.println("i2: "+ Arrays.toString(i2)); System.out.println("i3: "+ Arrays.toString(i3)); // Comparing arrays lexicographically System.out.println("\nArrays.compare(i1, i2): " + Arrays.compare(i1, i2)); System.out.println("Arrays.compare(i1, i3): " + Arrays.compare(i1, i3)); System.out.println("Arrays.compare(i3, i1): " + Arrays.compare(i3, i1)); // Comparing slicces of arrays lexicographically System.out.println("\nArrays.compare(i1, 0, 3, i3, 0, 3): " + Arrays.compare(i1, 0, 3, i3, 0, 3)); System.out.println("Arrays.compare(i1, 0, 3, i2, 0, 3): " + Arrays.compare(i1, 0, 3, i2, 0, 3)); System.out.println("Arrays.compare(i3, 0, 3, i1, 0, 3): " + Arrays.compare(i3, 0, 3, i1, 0, 3)); } }
Output :
i1: [2, 4, 6, 8, 10] i2: [2, 4, 6, 8, 10] i3: [2, 4, 12, 8, 10] Arrays.compare(i1, i2): 0 Arrays.compare(i1, i3): -1 Arrays.compare(i3, i1): 1 Arrays.compare(i1, 0, 3, i3, 0, 3): -1 Arrays.compare(i1, 0, 3, i2, 0, 3): 0 Arrays.compare(i3, 0, 3, i1, 0, 3): 1
1.3 Comparing arrays using mismatch() method
The group of methods mismatch() finds and returns the index of the first mismatch between two arrays. There are different overloads for boolean
, byte
, char
, double
, float
, int
, long
, short
and Object
arrays. We can also specify the start and end index in both arrays for checking the mismatch.
mismatch()
returns -1 if there is no mismatch.- If either array is
null
, it throws aNullPointerException
.
int mismatch(int[] a, int[] b) int mismatch (int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
Example :
public class ComparingArraysMismatchDemo { public static void main(String[] args) { int[] i1 = {2, 4, 6, 8, 10}; int[] i2 = {2, 4, 6, 8, 10}; int[] i3 = {2, 12, 6, 8, 10}; System.out.println("i1: "+ Arrays.toString(i1)); System.out.println("i2: "+ Arrays.toString(i2)); System.out.println("i3: "+ Arrays.toString(i3)); // Finding first mismatch System.out.println("\nArrays.mismatch(i1, i2): " + Arrays.mismatch(i1, i2)); System.out.println("Arrays.mismatch(i1, i3): " + Arrays.mismatch(i1, i3)); System.out.println("Arrays.mismatch(i3, i1): " + Arrays.mismatch(i3, i1)); // Finding first mismatch in slices System.out.println("\nArrays.mismatch(i1, 0, 3, i3, 0, 3): " + Arrays.mismatch(i1, 0, 3, i3, 0, 3)); System.out.println("Arrays.mismatch(i1, 0, 3, i2, 0, 3): " + Arrays.mismatch(i1, 0, 3, i2, 0, 3)); System.out.println("Arrays.mismatch(i3, 0, 3, i1, 0, 3): " + Arrays.mismatch(i3, 0, 3, i1, 0, 3)); } }
Output :
i1: [2, 4, 6, 8, 10] i2: [2, 4, 6, 8, 10] i3: [2, 12, 6, 8, 10] Arrays.mismatch(i1, i2): -1 Arrays.mismatch(i1, i3): 1 Arrays.mismatch(i3, i1): 1 Arrays.mismatch(i1, 0, 3, i3, 0, 3): 1 Arrays.mismatch(i1, 0, 3, i2, 0, 3): -1 Arrays.mismatch(i3, 0, 3, i1, 0, 3): 1
2. Conclusion
In this tutorial, we looked into several examples on newly introduced Java JDK 9 Arrays utility class methods for comparing arrays.
Also see Related Article :
1. Java Parallel array sorting