equals() and deepEquals() Method to Compare two Arrays in Java - GeeksforGeeks

28 Jan, 2021
Arrays. equals() method does not compare recursively if an array contains another array on other hand Arrays. deepEquals() method compare recursively if an array contains another array.
Arrays.equals(Object[], Object[]) 
Syntax :
public static boolean equals(int[] a, int[] a2)
Parameters :
a – one array to be tested for equality
a2 – the other array to be tested for equality
Returns : true if the two arrays are equal
Return True if the arrays are equal to one another.
If both arrays contain an equal number of elements and the same element in each index.
Also, if both arrays are null then they are considered equal.
Arrays.equals() method does not compare recursively if an array contains another array.
In all other cases, it returns False.
Array.deepEquals(Object[], Object[])
Syntax:
public static boolean deepEquals(Object[] o1, Object[] o2)

o1 = First Array to test for Equality
o2 = Second Array to test for Equality
Return True if the two arrays are equal to one another.
If both arrays contain equal number of elements and same element in each index.
Also, both arrays are null then they are considered as equal.
Moreover, if nested element is again array then nested array’s elements are also compared in iterative manner.
Arrays.deepEquals() method compare recursively if an array contains another array.
Main Differences :
If in any Array, an item is another Array itself then call to equals() goes to default java.lang.Object equals(), which compares reference of two Object and does not perform deep comparison and fails logical comparison in case of nested Array.
On the other hand Arrays.deepEquals() method performs a lot of checks and calls Arrays.equals() for non-array comparison and recursively call Arrays.deepEquals() for array type comparison, which allows it to compare nested array logically in Java.  
Note: It’s always suggested to use Arrays.equals() to compare non-nested Array and Arrays.deepEquals() to compare nested Array, as Array.equals() is faster than Arrays.deepEquals() in the case of non-nested Array.
Illustration of equals() in java:
Java
// Java program to compare two arrays// using .equals() method  import java.io.*;import java.util.*;  class GFG {    public static void main(String[] args)    {        int[] A = { 1, 2, 3, 4, 5 };        int[] B = { 1, 2, 3, 4, 5 };        int[] C = { 2, 1, 4, 3, 5 };                // Comparing two arrays A and B using .equals()        if (Arrays.equals(A,B))            System.out.println(                "Array A and Array B is equal");        else            System.out.println(                "Array A and Array B is not equal");          if (Arrays.equals(A, C))            System.out.println(                "Array A and Array C is equal");        else            System.out.println(                "Array A and Array C is not equal");    }}
Array A and Array B is equal
Array A and Array C is not equal
Below is Illustration of deepEquals() in java :-
Java
// Java program to compare two arrays// using .deepEquals() method  import java.io.*;import java.util.*;  class GFG {    public static void main(String[] args)    {        int[][] A            = { { 1, 2, 3 }, { 3, 4, 5 }, { 6, 7, 8 } };        int[][] B            = { { 1, 2, 3 }, { 3, 4, 5 }, { 6, 7, 8 } };        int[][] C            = { { 3, 4, 5 }, { 4, 5, 6 }, { 7, 2, 4 } };          // Comparing A and B arrays using .deepEquals() method        if (Arrays.deepEquals(A, B))            System.out.println(                "Array A and Array B is equal");        else            System.out.println(                "Array A and Array B is not equal");          if (Arrays.deepEquals(A, C))            System.out.println(                "Array A and Array C is equal");        else            System.out.println(                "Array A and Array C is not equal");    }}
Array A and Array B is equal
Array A and Array C is not equal
Java-Arrays
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Generics in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?