Pages

How to Compare Two Arrays in Java - String, Integer Array Example

Our task is to compare two arrays in Java and see if they are equal to each other or not. Of-course we can't compare an String array to an int array, which means two arrays are said to be equal if they are of same type, has same length, contains same elements and in same order. Now, you can write your own method for checking array equality or take advantage of Java's rich Collection API. Similar to what we have seen while printing array values in Java, java.util.Arrays class provides convenient methods for comparing array values. They take care of all four condition, I have mentioned above. In fact, Arrays class also provide deepEquals() method to compare two-dimensional array in Java. By the way it's not restricted to just two-dimensional and also valid for any multi-dimensional array.
In this article, we will see examples of comparing two String array, two Integer array and two multi dimensional array to learn how to use equals() and deepEquals() methods of java.util.Arrays class. Also difference between equals() and deepEquals() is a good Java interview question, and asked into of couple of fresher and mid level interviews.

Comparing two Integer Array in Java

In order to compare two integer array in Java, all you need to do is import java.util.Arrays class. This class contains two methods related to array comparison equals() and deepEquals(), both are overloaded to accept all primitive arrays and one version for accepting Object array. We need to use the equals(int[], int[]) method to compare our integer arrays, as shown below. As you can see, I have defined 3 integer arrays, each of same size but different numbers. int[]even and meEvenToo are equal to each other because they are of same type, same length, and contains same number in same order. While, int[] odd is not equal to int[] even, because though they are of same type and length, their individual content is different. As expected equals(int[], int[]) method of Arrays class produced correct result.

Comparing two String array in Java

As I said in previous section that java.util.Arrays class has overloaded equals() and deepEquals() to accept object[]. Since arrays are co-variant, you can pass String[] to a method which accepts an Object[], which is used to compare String array in Java. In this example we have three string array of length 2, out of three String[] numbers and digits are equal, because they also contain same values at similar index, but String[] numbers and numeric are unequal because there values at respective index is different. Remember, equality logic of individual object is handled by equals() method of that class itself, which means even string comparison inside array is case sensitive.

Comparing two multidimensional array in Java

Comparing multi-dimensional array is slightly different then comparing one dimensional arrays, because instead of Arrays.equals(), you need to use Arrays.deepEquals(). Tricky part is that, compiler will not catch your array of passing multi-dimensional array to Arrays.equals() method, which also doesn't throw any exception and simply return false. This can lead to subtle bugs, sometime very hard to catch. So, pay special care while comparing multi-dimensional array and ensure that you have used deepEquals() and not equals(). Here is an example of comparing two dimensional array in Java.

Source code of Java Program for Comparing two arrays

Here is the complete code sample of how to compare two arrays in Java. You don't need any third-party library, just JDK. If you are using any IDE like Eclipse or Netbeans, then just copy paste this code in a Java source file, whose name matches with public class name and run it. Alternatively you can also run this from command prompt.

That's all about how to compare two arrays in Java. By using equals() and deepEquals() method of java.util.Arrays class, you can easily compare any arbitrary array in Java. We have seen examples of comparing String arrays, int arrays and multi-dimensional arrays in Java programs. One thing to note is that, you must use deepEquals() method to compare two dimensional arrays in Java, using equals() in such case does not produce correct result. Do you know any other clever way to compare array in Java?