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:

Download  Run Code

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:

  • x and y are both arrays of object reference types, and Arrays.deepEquals(x, y) would return true.
  • x and y are arrays of the same primitive type, and the appropriate overloading of Arrays.equals(x, y) would return true.
  • x == y
  • x.equals(y) would return true.

The following program demonstrates the usage of Arrays.deepEquals() method for comparing an object array which contains a reference type:

Download  Run Code

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:

Download  Run Code

Output:

Arrays are equal

4. Comparing object array

The two object arrays are considered equal if:

  • Both array references are null or both are non-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.

Download  Run Code

Output:

Arrays are not equal

That’s all about checking for array equality in Java.