Close

Java - Arrays Equality

[Last Updated: Sep 1, 2018]

In this tutorial we will explore the different ways to compare two arrays.

Assuming that we have two arrays: array1 and array2 of any types:

  • array1. equals(array2) will be true only if they are the same instances (referencing same object)
  • Arrays. equals(array1, array2) will be true if each elements are equal (element1.equals(element2) is true) at same indexes. This method will not compare the nested arrays' elements.
  • Arrays. deep Equals(array1, array2): same as Array.equals() but will also check nested arrays recursively.

Let's see some examples.

equals(..) method

equals method on array instances will only return true if they are the same references (same as ==):

public class EqualsExample {

  public static void main(String[] args) {
      int[] array1 = {3, 5, 9};
      int[] array2 = {3, 5, 9};

      boolean equal = array1.equals(array2);
      System.out.printf("array1.equals(array2) : %s%n", equal);

      //equals will only be true if both are same objects
      boolean equal2 = array1.equals(array1);
      System.out.printf("array1.equals(array1) : %s%n", equal2);
  }
}

Output

array1.equals(array2) : false
array1.equals(array1) : true

Arrays.equals(..) method

public class ArraysEqualsExample {

  public static void main(String[] args) {
      int[] array1 = {3, 5, 9};
      int[] array2 = {3, 5, 9};

      boolean equal = Arrays.equals(array1, array2);
      System.out.printf("Arrays.equals(array1, array2) : %s%n", equal);

      Object[] array3 = {3, 5, new int[]{6, 7, 9}};
      Object[] array4 = {3, 5, new int[]{6, 7, 9}};

      boolean equal2 = Arrays.equals(array3, array4);
      System.out.printf("Arrays.equals(array3, array4) : %s%n", equal2);
  }
}

Output

Arrays.equals(array1, array2) : true
Arrays.equals(array3, array4) : false

Arrays.deepEquals(..) method

public class ArraysDeepEqualsExample {

  public static void main(String[] args) {
      Object[] array1 = {3, 5, 9};
      Object[] array2 = {3, 5, 9};

      boolean equal = Arrays.deepEquals(array1, array2);
      System.out.printf("Arrays.deepEquals(array1, array2) : %s%n", equal);

      //nested arrays
      Object[] array3 = {3, 5, new int[]{6, 7, 9}};
      Object[] array4 = {3, 5, new int[]{6, 7, 9}};

      boolean equal2 = Arrays.deepEquals(array3, array4);
      System.out.printf("Arrays.deepEquals(array3, array4) : %s%n", equal2);

      //more nested levels
      Object[] array5 = {3, 5, new Object[]{6, 7, new int[]{11, 13}}};
      Object[] array6 = {3, 5, new Object[]{6, 7, new int[]{11, 13}}};

      boolean equal3 = Arrays.deepEquals(array5, array6);
      System.out.printf("Arrays.deepEquals(array5, array6) : %s%n", equal3);
  }
}

Output

Arrays.deepEquals(array1, array2) : true
Arrays.deepEquals(array3, array4) : true
Arrays.deepEquals(array5, array6) : true

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.3.9

Java Arrays Equality Examples Select All Download
  • java-array-equality
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ArraysDeepEqualsExample.java

    See Also