Javatpoint Logo
Javatpoint Logo

How to Compare Two Arrays in Java?

In Java, we can compare two arrays by comparing each element of the array. Java Arrays class provides two predefined methods that is used to compare two arrays in Java.

In this section, we will learn how to compare two Arrays using Arrays.equals() method and Arrays.deepEquals() method. Along with this, we will also learn how to perform a deep comparison between the two arrays with proper examples.

Two arrays are equal if:

  • They are of the same type.
  • They have an equal number of elements.
  • Corresponding pairs of elements in both arrays must be equal.
  • The order of elements must be the same.
  • Two array references are equal if they are null.

Before moving to the topic, first, consider the following example and guess the output.

ArrayEqualsExample1.java

Output:

Arrays are not equal.

In the above example, a1 and a2 are the two references of two different objects. When we compare two reference variables, we get the output Arrays are not equal, while both the arrays are of equal length, and having the same elements. We are not getting the desired output because the equals (==) operator compare them as an object.

Now, we have only an option to compare two arrays, i.e. compare the content (elements) of the array. Let's see how to compare array contents.

It will be good if we compare the elements one by one. To compare the content of the array Java Arrays class provides the following two methods to compare two arrays:

  • equals() Method
  • deepEquals() Method

Arrays.equals() Method

Java Arrays class provides the equals() method to compare two arrays. It iterates over each value of an array and compares the elements using the equals() method.

Syntax:

It parses two arrays a1 and a2 that are to compare. The method returns true if arrays are equal, else returns false. The Arrays class has a list of overloaded equals() method for different primitive types and one for an Object type.

Note: While using the array of objects, don't forget to override the equals() method. Otherwise, we will get the output returned by the equals() method of the Object class.

ArrayEqualsExample2.java

Output:

Arrays are equal.

Let's see another example.

ArrayEqualsExample3.java

Output:

Are array1 and array2 equal? false
Are array1 and array3 equal? true

We see that the Arrays.equals() method compares the elements of the array. Here, a question arises that what if an array has nested array or some other references which refer to different object but have the same values.

Let's understand it through the following example.

ArrayEqualsExample4.java

Output:

Arrays are not equal.

In the above example, we see that the equals() method is not able to perform a deep comparison. To resolve this problem, the Java Arrays class provides another method deepEquals() that deeply compares the two arrays.

Arrays.deepEquals() Method

Java Arrays class provides another method deepEquals() to deeply compare the array. Here, deeply compare means it can compare two nested arrays of arbitrary depth. Two arrays object reference e1 and e2 are deeply equal if they hold any of the following condition:

  • e1=e2
  • equals(e2) returns true.
  • If e1 and e2 are both the same primitive type the overloading of the method Arrays.equals(e1, e2) returns true.
  • If e1 and e2 are both arrays of object reference types, the method Arrays.deepEquals(e1, e2) returns true.

Syntax:

The method parses the two arrays a1 and a2 that is to compare. It returns true if both arrays are deeply equal, else returns false.

Let's create a program and deeply compare two arrays using the deepEquals() method of the Arrays class.

ArrayEqualsExample5.java

Output:

Arrays are equal.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA