Java Arrays Comparison: Use equals or deepEquals?

31 / May / 2016 by Sandeep Gupta 0 comments

The equals Method return True if the two arrays are equal to one another if both arrays contain equal number of elements and corresponding elements are also equal. If both arrays are null then they are also considered as equal.  The equals method is used with Hashcode method to find the bucket of arrays. Two arrays are equal only if their hashCode is also equal.

Limitation: The methods hashCode and equals both are using first level elements which means if any element is again of Array type then that is ignored. Child level arrays are not considered.

The deepEquals Method return True if the two arrays are equal to one another if both arrays contain equal number of elements and corresponding elements are also equal moreover if child element is again array then child array’s elements are also compared in iterative manner. If both arrays are null then they are also considered as equal. The deepEquals method is used with deepHashCode method to find the bucket of arrays. Two arrays are equal only if their deepHashCode is also equal, which calculates the hashcode in iterative manner if any child element is of Array type.

Note: If the arrays don’t have any child array then above both approaches will give same result.

[java]
package ds.arrays;

import java.util.Arrays;

public class DeepEquals {
public static void main(String[] args) {
String[] string1 = new String[]{"first","second"};
String[] string2 = new String[]{"first","second"};

Integer[] num1 = new Integer[]{1};
Integer[] num2 = new Integer[]{2};

Object[] object1 = new Object[]{"first", "second", new String[]{"third"}};
Object[] object2 = new Object[]{"first", "second", new String[]{"third"}};

/* Same Behavior when there are no nested array type child elements */
System.out.println("Compare string1 and string2 Using HashCode and Equals: " + Arrays.equals(string1, string2));
System.out.println("Compare string1 and string2 Using DeepHashCode and DeepEquals: " + Arrays.deepEquals(string1, string2));

// Marking two arrays as null
num1 = null;
num2 = null;
/* Same Behavior when arrays are null */
System.out.println("Compare (null arrays) num1 and num2 Using HashCode and Equals: " + Arrays.equals(num1, num2));
System.out.println("Compare (null arrays) num1 and num2 Using DeepHashCode and DeepEquals: " + Arrays.deepEquals(num1, num2));

/* With nested objects, Behavior is totally different */
System.out.println("Compare object1 and object2 Using HashCode and Equals: " + Arrays.equals(object1, object2));
System.out.println("Compare object1 and object2 Using DeepHashCode and DeepEquals: " + Arrays.deepEquals(object1, object2));

}
}
[/java]

Output:
Compare string1 and string2 Using HashCode and Equals: true                                                                                               
Compare string1 and string2 Using DeepHashCode and DeepEquals: true                                                                                       
Compare (null arrays) num1 and num2 Using HashCode and Equals: true                                                                                       
Compare (null arrays) num1 and num2 Using DeepHashCode and DeepEquals: true                                                                               
Compare object1 and object2 Using HashCode and Equals: false                                                                                              
Compare object1 and object2 Using DeepHashCode and DeepEquals: true
FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *