Open In App

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

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 :



Returns : true if the two arrays are equal

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

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 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");
    }
}

Output
Array A and Array B is equal
Array A and Array C is not equal

Below is Illustration of deepEquals() in 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");
    }
}

Output
Array A and Array B is equal
Array A and Array C is not equal

Article Tags :