Signup/Sign In

Java Arrays deepEquals() Method

In this tutorial, we will learn about deepEquals() method in the Arrays class. This method is slightly different than the equals() method because this method works correctly for one-dimensional and multi-dimensional arrays.

Syntax

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

This method will return true only in the following cases:

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

Example of checking whether both arrays are equal or not using deepEquals() method

In the example given below, we are comparing two nested arrays deepEquals() method checks each array element from array1 to array2 and nested elements also. Finally, it will return true or false based on the example. In our case, both arrays are not equal so it is returning a false value.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		int array1[][] = { 
				{ 5, 2 }, 
				{ 9, 12 }, 
				{ 9, 1 } }; 

		int array2[][] = { 
				{ 47, 8 }, 
				{ 12, 50 }, 
				{ 13, 14 } }; 

		if(Arrays.deepEquals(array1, array2))
		{
			System.out.println("Both arrays are equal");
		}
		else
		{
			System.out.println("Both arrays are not equal");
		}
	} 
}


Both arrays are not equal

Example: deepEquals() Method to check equality of array of an objects

In the example given below, we illustrated that deepEquals() method can be also used to check whether two arrays of an object are equal or not. While applying this method we need to take special care of the overriding equals method because the compiler should on the basis of which parameters it is going to decide whether both objects are equal or not.

import java.util.Arrays;
class Student
{
	String stud_name;
	int roll_no;
	Student(String stud_name, int roll_no){
		this.stud_name = stud_name;
		this.roll_no = roll_no;
	}
	//overrifing equals method
	public boolean equals(Object obj) 
    {
        Student s = (Student)obj; 
        return (this.stud_name == s.stud_name && this.roll_no==s.roll_no); 
    } 
}
class StudyTonight { 
	public static void main(String args[]) 
	{ 
		Student array1[][] = { 
				{ new Student("AAA",123), new Student("BBB",789) }, 
				{ new Student("CCC",183), new Student("DDD",445) }, 
				{ new Student("EEE",693), new Student("FFF",348) } }; 

		Student array2[][] = { 
				{ new Student("AAA",123), new Student("BBB",789) }, 
				{ new Student("CCC",183), new Student("DDD",445) }, 
				{ new Student("EEE",693), new Student("FFF",348) } }; 		

		if(Arrays.deepEquals(array1, array2))
		{
			System.out.println("Both arrays are equal");
		}
		else
		{
			System.out.println("Both arrays are not equal");
		}
	} 
}


Both arrays are equal

Conclusion

In this tutorial, we learned how to use deepEquals() method to compare arrays. deepEquals() method is different from the equals() method because this method works with nested array as well as an array of objects. In the case of an array of objects, we need to override an equals() method so that compiler should know the criteria for equality of objects.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.