Tuesday, 7 October 2014

Compare Arrays : deepEquals Vs equals

Java.util.Arrays class provides two methods 'equals' and 'deepEquals' to compare two Arrays. The only difference between equals and deepEquals is,  deepEquals method can compare nested arrays of arbitrary depth, where as equals method can’t.

As per Javadoc, The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.

As per Javadoc, 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.

Code snippet of equals method looks like below. It does not check if element is Array type or not and simply calls equals(), which in case of array act similar to == operator (Which compares whether the references pointing to same object or not).

public static boolean equals(Object[] a, Object[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

        int length = a.length;
        if (a2.length != length)
            return false;

        for (int i=0; i<length; i++) {
            Object o1 = a[i];
            Object o2 = a2[i];
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }

        return true;
    }


Code snippet of deepEquals method looks like below. If the element is of type Array, then deepEquals method recursively checks for the equality of the elements in that Array.

public static boolean deepEquals(Object[] a1, Object[] a2) {
        if (a1 == a2)
            return true;
        if (a1 == null || a2==null)
            return false;
        int length = a1.length;
        if (a2.length != length)
            return false;

        for (int i = 0; i < length; i++) {
            Object e1 = a1[i];
            Object e2 = a2[i];

            if (e1 == e2)
                continue;
            if (e1 == null)
                return false;

            // Figure out whether the two elements are equal
            boolean eq;
            if (e1 instanceof Object[] && e2 instanceof Object[])
                eq = deepEquals ((Object[]) e1, (Object[]) e2);
            else if (e1 instanceof byte[] && e2 instanceof byte[])
                eq = equals((byte[]) e1, (byte[]) e2);
            else if (e1 instanceof short[] && e2 instanceof short[])
                eq = equals((short[]) e1, (short[]) e2);
            else if (e1 instanceof int[] && e2 instanceof int[])
                eq = equals((int[]) e1, (int[]) e2);
            else if (e1 instanceof long[] && e2 instanceof long[])
                eq = equals((long[]) e1, (long[]) e2);
            else if (e1 instanceof char[] && e2 instanceof char[])
                eq = equals((char[]) e1, (char[]) e2);
            else if (e1 instanceof float[] && e2 instanceof float[])
                eq = equals((float[]) e1, (float[]) e2);
            else if (e1 instanceof double[] && e2 instanceof double[])
                eq = equals((double[]) e1, (double[]) e2);
            else if (e1 instanceof boolean[] && e2 instanceof boolean[])
                eq = equals((boolean[]) e1, (boolean[]) e2);
            else
                eq = e1.equals(e2);

            if (!eq)
                return false;
        }
        return true;
    }


Example Of deepEquals Vs equals
import java.util.Arrays;

public class CompareArrays {
    public static void main(String args[]){
        Integer arr1[] = {1,2,3,4};
        Integer arr2[] = {1,2,3,4};

        System.out.println("equals : Is arr1 and arr2 equal " + Arrays.equals(arr1, arr2));
        System.out.println("deepEquals : Is arr1 and arr2 equal " + Arrays.deepEquals(arr1, arr2));

        Integer arr3[][] = {{1,2},{3,4}};
        Integer arr4[][] = {{1,2},{3,4}};

        System.out.println("equals : Is arr1 and arr2 equal " + Arrays.equals(arr3, arr4));
        System.out.println("deepEquals : Is arr1 and arr2 equal " + Arrays.deepEquals(arr3, arr4));

    }
}


Output
equals : Is arr1 and arr2 equal true
deepEquals : Is arr1 and arr2 equal true
equals : Is arr1 and arr2 equal false
deepEquals : Is arr1 and arr2 equal true



You can observe, equals method returns false in case of multi-dimensional Arrays.

No comments:

Post a Comment