Arrays.deepEquals() in Java with Examples (original) (raw)
Last Updated : 25 Nov, 2024
Arrays.deepEquals()
method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional, are equal or not. It compares arrays element by element, even when dealing with nested arrays.
**Example:
Below is a simple example that uses Arrays.deepEquals()
to check if two multidimensional arrays are equal.
Java `
import java.util.Arrays;
public class ArraysDeepEquals {
public static void main(String[] args) {
// three 2D integer arrays for comparison
int[][] arr1 = { {1, 2}, {3, 4} };
int[][] arr2 = { {1, 2}, {3, 4} };
int[][] arr3 = { {5, 6}, {7, 8} };
// Compare arr1 and arr2
System.out.println("arr1 equals with arr2: " + Arrays.deepEquals(arr1, arr2));
// Compare arr2 and arr3
System.out.println("arr2 equals with arr3: " + Arrays.deepEquals(arr2, arr3));
}
}
`
Output
arr1 equals with arr2: true arr2 equals with arr3: false
Table of Content
Syntax of Arrays.deepEquals() method
public static boolean deepEquals(Object[] o1, Object[] o2)
**Parameter:
- **o1: This is the first array to test for equality.
- **o2: This is the second array to test for equality.
**Return Type: It returns true,
if the two arrays are deeply equal (all elements and nested arrays are equal), otherwise it will return false.
Examples of Arrays.deepEquals() in Java
Using Arrays.deepEquals()
with Multidimensional Arrays of Integers
In this example, we will check if two-dimensional arrays of integers are equal at all levels using Arrays.deepEquals()
.
Java `
// Java program to demonstrate working of // Arrays.deepEquals() with integer arrays import java.util.Arrays;
public class GFG { public static void main(String[] args) {
int a1[][] = { { 10, 20 },
{ 30, 40 },
{ 50, 60 } };
int a2[][] = { { 50, 60 },
{ 10, 20 },
{ 30, 40 } };
int a3[][] = { { 10, 20 },
{ 30, 40 },
{ 50, 60 } };
// Check if a1 and a2 are equal
System.out.println("a1 is equal to a2: " + Arrays.deepEquals(a1, a2));
// Check if a2 and a3 are equal
System.out.println("a2 is equal to a3: " + Arrays.deepEquals(a2, a3));
// Check if a1 and a3 are equal
System.out.println("a1 is equal to a3: " + Arrays.deepEquals(a1, a3));
}
}
`
Output
a1 is equal to a2: false a2 is equal to a3: false a1 is equal to a3: true
Using Arrays.deepEquals()
with Arrays of User-Defined Objects
In this example, we will check if arrays of user-defined Employee
objects are equal by overriding the equals() method to define the equality of different parameters in a user-defined class.
Java `
// Java program to demonstrate working of // Arrays.deepEquals() // with arrays of user-defined objects import java.util.Arrays;
public class ArraysDeepEquals2 { public static class Employee {
int i;
String n;
public Employee(int i, String n) {
this.i = i;
this.n = n;
}
// Override equals() to define equality
// based on id and name
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee e = (Employee) o;
return i == e.i && n.equals(e.n);
}
}
public static void main(String[] args) {
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") } };
// Check if e1 and e2 are equal
System.out.println("e1 is equal to e2: " + Arrays.deepEquals(e1, e2));
// Check if e2 and e3 are equal
System.out.println("e2 is equal to e3: " + Arrays.deepEquals(e2, e3));
// Check if e1 and e3 are equal
System.out.println("e1 is equal to e3: " + Arrays.deepEquals(e1, e3));
}
}
`
Output
e1 is equal to e2: true e2 is equal to e3: false e1 is equal to e3: false
**Equals() vs deepEquals()
- Arrays.equals() method works correctly on an single dimensional array, but it cannot check the equality of a multidimensional array.
- **Arrays.deepEquals() method works on all arrays irrespective of the dimension.