Open In App

Java.util.Arrays.deepEquals() in Java


Arrays.deepEquals() is used to check whether two arrays of single dimensional or multi-dimensional arrays are equal or not. It can compare two nested arrays (i.e. multidimensional array), irrespective of its dimension.

Syntax:

public static boolean deepEquals(Object[] o1, Object[] o2)

o1 = First Array to test for Equality
o2 = Second Array to test for Equality

Returns true if two array are equal




// Java program to demonstrate working of deepEquals.
import java.util.Arrays;
public class GFG {
public static void main(String[] args)
    {
        int a1[][] = { { 10, 20 },
                       { 40, 50 },
                       { 60, 70 } };
  
        int a2[][] = { { 30, 20 },
                       { 10, 0 },
                       { 60, 80 } };
  
        int a3[][] = { { 10, 20 },
                       { 40, 50 },
                       { 60, 70 } };
        System.out.println("Check if a1 is equal to a2 : "
                           + Arrays.deepEquals(a1, a2));
  
        System.out.println("Check if a2 is equal to a3 : "
                           + Arrays.deepEquals(a2, a3));
  
        System.out.println("Check if a1 is equal to a3 : "
                           + Arrays.deepEquals(a1, a3));
    }
}

Output:



Check if a1 is equal to a2 : false
Check if a2 is equal to a3 : false
Check if a1 is equal to a3 : true

We can even use deepEquals() to test the equality of array of Object of the user define class. Refer to the example below
We should override the equals method to define the equality of the different parameters in a user defined class.




// Java program to demonstrate working of deepEquals
// for arrays of user defined obj.
import java.util.Arrays;
public class GFG {
public static class Employee {
  
        int Eid;
        String Ename;
  
    public Employee(int Eid, String Ename)
        {
            this.Eid = Eid;
            this.Ename = Ename;
        }
  
        // Overriding the equals()
    public boolean equals(Object obj)
        {
  
            // type casting obj to Employee
            Employee s = (Employee)obj;
            return (this.Eid == s.Eid && this.Ename.equals(s.Ename));
        }
    } public static void main(String args[])
    {
        // Creating an array of objects of user defined class.
        Employee e1[][] = { { new Employee(10, "Geek1"),
                              new Employee(11, "Geek2") },
                            { new Employee(12, "Geek3"),
                              new Employee(13, "Geek4") } };
  
        Employee e2[][] = { { new Employee(10, "Geek1"),
                              new Employee(11, "Geek2") },
                            { new Employee(12, "Geek3"),
                              new Employee(13, "Geek4") } };
  
        Employee e3[][] = { { new Employee(12, "Geek2"),
                              new Employee(25, "Geek4") },
                            { new Employee(15, "Geek3"),
                              new Employee(30, "Geek1") } };
  
        System.out.println("Check if e1 is equal to e2 : "
                           + Arrays.deepEquals(e1, e2));
  
        System.out.println("Check if e2 is equal to e3 : "
                           + Arrays.deepEquals(e2, e3));
  
        System.out.println("Check if a1 is equal to a3 : "
                           + Arrays.deepEquals(e1, e3));
    }
}

Output:

Check if e1 is equal to e2 : true
Check if e2 is equal to e3 : false
Check if a1 is equal to a3 : false

Equals() vs deepEquals()

Though Arrays.equals() works correctly on an single dimensional array but it cannot check the equality of a multidimensional arrays.
While Arrays.deepEquals() work on all arrays irrespective of the dimension.

Reference :
https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#deepEquals-java.lang.Object:A-java.lang.Object:A-


Article Tags :