Arrays.equals() in Java with Examples
The Arrays.equals() method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional array are equal or not.
Example:
Below is a simple example that uses Arrays.equals() method to check if two arrays of integers are equal or not.
// Java program to demonstrate
// working of Arrays.equals()
import java.util.Arrays;
public class ArraysEquals {
public static void main(String[] args) {
// create different integers arrays
int[] arr1 = {1, 2, 3, 4};
int[] arr2 = {1, 2, 3, 4};
int[] arr3 = {1, 2, 4, 3};
System.out.println("arr1 equals to arr2: " +
Arrays.equals(arr1, arr2));
System.out.println("arr1 equals to arr3: " +
Arrays.equals(arr1, arr3));
}
}
Output
arr1 equals to arr2: true arr1 equals to arr3: false
Table of Content
Syntax of Arrays.equals() method
public static boolean equals(int[] a, int[] a2)
Parameters:
- a: The first
int[]
array to be compared. - a2: The second
int[]
array to be compared.
Returns Type: boolean
: Returns true
if the two arrays are equal, otherwise false.
Examples of Arrays.equals() in Java
Compare Arrays of User-Defined Objects Using Arrays.equals()
In this example, we will compare arrays of Student
objects for equality by overriding the equals()
method to define how students are considered equal based on their attributes.
// Java program to demonstrate
// working of Arrays.equals()
// for user defined objects
import java.util.Arrays;
public class ArraysEquals {
public static void main (String[] args) {
Student [] arr1 = {new Student(1, "a", "MP"),
new Student(2, "b", "UP"),
new Student(3, "c", "Delhi")};
Student [] arr2 = {new Student(1, "a", "MP"),
new Student(2, "b", "UP"),
new Student(3, "c", "Delhi")};
Student [] arr3 = {new Student(1, "a", "MP"),
new Student(2, "b", "UP"),
new Student(3, "c", "Jaipur"),
};
System.out.println("arr1 equals to arr2: " +
Arrays.equals(arr1, arr2));
System.out.println("arr1 equals to arr3: " +
Arrays.equals(arr1, arr3));
}
}
// class to represent a student
class Student
{
int r;
String n, a;
// Constructor
public Student(int r, String n,
String a)
{
this.r = r;
this.n = n;
this.a = a;
}
@Override
public boolean equals(Object o) {
// typecast o to Student so that we can compare students
Student s = (Student) o;
return this.r == s.r && this.n.equals(s.n)
&& this.a.equals(s.a);
}
}
Output
arr1 equals to arr2: true arr1 equals to arr3: false
Compare Multidimensional Arrays with Arrays.equals()
In this example, we will use both Arrays.equals() and Arrays.deepEquals() method to compare two multidimensional arrays. It returns true, if the two specified arrays are deeply equal to each other, otherwise it will return false.
import java.util.Arrays;
public class ArrayEqual2 {
public static void main(String[] args) {
// 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
Note: Arrays.equals() method can be only performed to 1-D arrays and hence it doesn’t work for multidimensional arrays.
Array.equals() vs. Arrays.deepEquals()
Arrays.equals()
method compares arrays element by element, but this method only works for single-dimensional arrays or arrays of objects.Arrays.deepEquals()
method compares arrays recursively by making it suitable for multi-dimensional arrays.