Open In App

Java.util.Arrays.equals() in Java with Examples

Today we are going to discuss the simplest way to check whether two arrays are equal or not. 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. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null. Arrays class in java provide the method Arrays.equals() to check whether two arrays are equal or not.

Syntax :
public static boolean equals(int[] a, int[] a2)
Parameters :
a - one array to be tested for equality
a2 - the other array to be tested for equality
Returns : 
true if the two arrays are equal

Other Variants:






// Java program to demonstrate working of Arrays.equals()
 
import java.util.Arrays;
 
public class ArrayEqualDemo
{
    public static void main(String[] args)
    {
        // Let us create different integers arrays
        int[] arr1 = new int [] {1, 2, 3, 4};
        int[] arr2 = new int [] {1, 2, 3, 4};
        int[] arr3 = new int [] {1, 2, 4, 3};
         
        System.out.println("is arr1 equals to arr2 : " +
                                Arrays.equals(arr1, arr2));
        System.out.println("is arr1 equals to arr3 : " +
                                Arrays.equals(arr1, arr3));
    }
}

Output:

is arr1 equals to arr2 : true
is arr1 equals to arr3 : false

We can also use Arrays.equals() for checking equality of array of objects of user defined class.Have a look at last variant of the Arrays.equals() method 



Note :- In case of arrays of Objects, you must override equals method to provide your own definition of equality, otherwise you will get output depends on what equals() method of Object class returns. In the program below, we are checking equality of rollno, name, and address for a student. 




// Java program to demonstrate working of Arrays.equals()
// for user defined objects.
 
import java.util.Arrays;
 
 
public class ArrayEqualDemo
{
    public static void main (String[] args)
    {
        Student [] arr1 = {new Student(111, "bbbb", "london"),
                        new Student(131, "aaaa", "nyc"),
                        new Student(121, "cccc", "jaipur")};
         
        Student [] arr2 = {new Student(111, "bbbb", "london"),
                        new Student(131, "aaaa", "nyc"),
                        new Student(121, "cccc", "jaipur")};
         
        Student [] arr3 = {new Student(111, "bbbb", "london"),
                        new Student(121, "dddd", "jaipur"),
                        new Student(131, "aaaa", "nyc"),
                        };
         
        System.out.println("is arr1 equals to arr2 : " +
                                    Arrays.equals(arr1, arr2));
        System.out.println("is arr1 equals to arr3 : " +
                                    Arrays.equals(arr1, arr3));   
    }   
}
 
// A class to represent a student.
class Student
{
    int rollno;
    String name, address;
 
    // Constructor
    public Student(int rollno, String name,
                            String address)
    {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
     
    @Override
    public boolean equals(Object obj) {
         
        // typecast obj to Student so that we can compare students
        Student s = (Student) obj;
         
        return this.rollno == s.rollno && this.name.equals(s.name)
                                && this.address.equals(s.address);
    }
}

Output:

is arr1 equals to arr2 : true
is arr1 equals to arr3 : false

Note:  String.equals() can be only performed to 1-D arrays and hence it doesn’t work for multidimensional arrays. Use Arrays.deepEquals(Object[], Object[]) instead , it returns true if the two specified arrays are deeply equal to each other.




import java.util.Arrays;
 
public class ArrayEqualDemo_2 {
    public static void main(String[] args)
    {
        // Let us create array of arrays
        int[][] arr1 = { { 0, 1 }, { 1, 0 } };
        int[][] arr2 = { { 0, 1 }, { 1, 0 } };
 
        System.out.println("is arr1 equals to arr2 : "
                           + Arrays.equals(arr1, arr2));
        System.out.println("is arr1 deepequals to arr2 : "
                           + Arrays.deepEquals(arr1, arr2));
    }
}

Output:

is arr1 equals to arr2 : false
is arr1 deepequals to arr2 : true

Article Tags :