Check for array equality in Java
This post will discuss how to check for array equality in Java. Two arrays are considered to be equal if they contain the same elements in the same order.
Array equality in Java is a concept that refers to the comparison of two arrays to determine if they have the same elements in the same order. We cannot use the == operator to compare two arrays. This operator checks if both references point to the same array object in memory. It returns true if both arrays refer to the same array object and returns false otherwise. Also, comparing two arrays with the equals() method will return true only when comparing against the same array instance. This is because their equals() implementations is inherited from java.lang.Object. This post will discuss how we can compare two arrays in Java.
1. Using Arrays.equals() method
Java Collections framework provides Arrays.equals() utility method for comparing arrays for equality. This method compares two arrays of primitive types or objects by iterating over each element and using the equals() method of the corresponding elements. It returns true if both arrays are null, have the same length, and contain equal elements in the same order. It returns false otherwise. The method is overloaded for all primitive types and objects. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] a = { 1, 2, 3, 4 }; int[] b = { 1, 2, 3, 4 }; if (Arrays.equals(a, b)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } } |
Output:
Arrays are equal
The Arrays.equals() method will fail when the array contains a reference type since this method will internally call equals() on the reference type to determine equality.
2. Using Arrays.deepEquals() method
To compare nested arrays of arbitrary depth, we can use the Arrays.deepEquals() instead, which returns true if the two specified arrays are deeply equal. It uses the deepEquals() method of the corresponding elements to compare them recursively. Two array references are considered deeply equal if both are null or refer to arrays containing the same number of elements. All corresponding pairs of elements in the two arrays are deeply equal. Two possibly null elements x and y are deeply equal if any of the following conditions hold:
xandyare both arrays of object reference types, andArrays.deepEquals(x, y)would returntrue.xandyare arrays of the same primitive type, and the appropriate overloading ofArrays.equals(x, y)would returntrue.x == yx.equals(y)would returntrue.
The following program demonstrates the usage of Arrays.deepEquals() method for comparing an object array which contains a reference type:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Arrays; class Main { public static void main(String[] args) { // String arrays String[] a = { "public", "static", "void", "main" }; String[] b = { "public", "static", "void", "main" }; // object array contains a reference type Object[] A = { a }; Object[] B = { b }; if (Arrays.deepEquals(A, B)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } } |
Output:
Arrays are equal
3. Using noneMatch() method
We can also write our own method to compare two primitives arrays using the noneMatch() method of the respective primitive stream class. This method returns true if no elements of the primitive stream matches with the supplied predicate, otherwise false. We can also use a for loop to get more control over how we want to compare the arrays, such as ignoring the order or case of the elements. For example, we can use something like this to check for array equality in a primitive integer array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.util.stream.IntStream; class Main { private static boolean compareIntArrays(int[] a, int[] b) { // 1. Check for the same array reference if (a == b) { return true; } // 2. Check if either array is null if (a == null || b == null) { return false; } // 3. Check if both arrays have the same length or not if (a.length != b.length) { return false; } // 4. Finally, compare array values at each index return IntStream.range(0, a.length).noneMatch(i -> a[i] != b[i]); } public static void main(String[] args) { int[] a = { 1, 2, 3, 4 }; int[] b = { 1, 2, 3, 4 }; if (compareIntArrays(a, b)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } } |
Output:
Arrays are equal
4. Comparing object array
The two object arrays are considered equal if:
- Both array references are
nullor both arenon-null, and - Both arrays have the same type, and
- Both arrays contain the same number of elements, and
- All corresponding pairs of objects in the two arrays are equal.
Here’s a generic method to check for object array equality in Java. It accepts arrays to be tested for equality and returns true if the two arrays are equal and false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.Objects; import java.util.stream.IntStream; class Main { private static <U, V> boolean compareObjectArrays(U[] a, V[] b) { // 1. Check for the same array reference if (a == b) { return true; } // 2. Check if either array is null if (a == null || b == null) { return false; } // 3. Check if both arrays have the same type or not if (!a.getClass().equals(b.getClass())) { return false; } // 4. Check if both arrays have the same length or not if (a.length != b.length) { return false; } // 5. Finally, compare array values at each index return IntStream.range(0, a.length).allMatch(i -> Objects.equals(a[i], b[i])); } public static void main(String[] args) { String[] a = { "public", "static", "void", "main" }; String[] b = { "public", "static", "void", "fun" }; if (compareObjectArrays(a, b)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } } |
Output:
Arrays are not equal
That’s all about checking for array equality in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)