Open In App

Java.util.Arrays.deepEquals() in Java

Last Updated : 07 Aug, 2017
Improve
Improve
Like Article
Like
Save
Share
Report


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.

  • Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.
  • Two possibly null elements e1 and e2 are deeply equal if any of the following conditions hold:

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

    Note that this definition permits null elements at any depth.

  • It is a method of Arrays Class

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-



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads