This post will check if two string arrays are equal or not in Java. The two string arrays are considered equal if both arrays have the same length and contain the same elements in the same order.

1. Comparing Single Dimensional Arrays

A naive solution is to write our own method for checking the equality of the string array.

Download  Run Code

Output:

Both arrays are equal

 
Here’s an equivalent version using the Stream API.

Download  Run Code

Output:

Both arrays are equal

 
The java.util.Arrays class provides two convenient methods for array comparison – equals() and deepEquals(). We can use either method for string array comparison.

Download  Run Code

Output:

Both arrays are not equal

2. Comparing Multi-Dimensional Arrays

Like the single-dimensional arrays, we can write our own method for checking the equality of multidimensional arrays.

Download  Run Code

Output:

Both arrays are not equal

 
Arrays.equals() method will not work for multidimensional arrays. We should use Arrays.deepEquals() method instead.

Download  Run Code

Output:

equals() returns false
deepEquals() returns true

That’s all about comparing two string arrays for equality in Java.