Compare two string arrays for equality in Java
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.
|
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 |
class Main { public static boolean checkEquality(String[] s1, String[] s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } int n = s1.length; if (n != s2.length) { return false; } for (int i = 0; i < n; i++) { if (!s1[i].equals(s2[i])) { return false; } } return true; } public static void main(String[] args) { String[] s1 = { "A", "B", "C" }; String[] s2 = { "A", "B", "C" }; if (checkEquality(s1, s2)) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
Output:
Both arrays are equal
Here’s an equivalent version using the Stream API.
|
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 |
import java.util.stream.IntStream; class Main { public static boolean checkEquality(String[] s1, String[] s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null || s1.length != s2.length) { return false; } return IntStream.range(0, s1.length).allMatch(i -> s1[i].equals(s2[i])); } public static void main(String[] args) { String[] s1 = { "A", "B", "C" }; String[] s2 = { "A", "B", "C" }; if (checkEquality(s1, s2)) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
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.
|
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) { String[] s1 = { "A", "B", "C" }; String[] s2 = { "X", "Y", "Z" }; if (Arrays.equals(s1, s2)) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
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.
|
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 45 46 47 48 49 50 51 52 53 54 55 56 |
class Main { public static boolean checkEquality(String[][] s1, String[][] s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } int n = s1.length; if (n != s2.length) { return false; } for (int i = 0; i < n; i++) { if (s1[i].length != s2[i].length) { return false; } for (int j = 0; j < s1[i].length; j++) { if (!s1[i][j].equals(s2[i][j])) { return false; } } } return true; } public static void main(String[] args) { String[][] s1 = { {"A", "B", "C"}, {"X", "Y", "Z"} }; String[][] s2 = { {"A", "B", "C"}, {"X", "Y", "Z"} }; if (checkEquality(s1, s2)) { System.out.printf("Both arrays are equal"); } else { System.out.printf("Both arrays are not equal"); } } } |
Output:
Both arrays are not equal
Arrays.equals() method will not work for multidimensional arrays. We should use Arrays.deepEquals() method instead.
|
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[][] s1 = { {"A", "B", "C"}, {"X", "Y", "Z"} }; String[][] s2 = { {"A", "B", "C"}, {"X", "Y", "Z"} }; System.out.println("equals() returns " + Arrays.equals(s1, s2)); System.out.println("deepEquals() returns " + Arrays.deepEquals(s1, s2)); } } |
Output:
equals() returns false
deepEquals() returns true
That’s all about comparing two string arrays for 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 :)