Arrays.equals()
is a static method in Java’s java.util.Arrays
class that is used to check if two arrays are equal. It provides a convenient way to compare arrays element-by-element for equality. The method is overloaded to handle arrays of different types, including primitive types (e.g., int
, char
, double
) and object types.
Arrays.equals()
==
operator.equals()
method of the objects.Arrays.equals()
returns false
.null
, Arrays.equals()
returns true
.null
and the other is not, it returns false
.Here are the main overloaded variants of the Arrays.equals()
method:
public static boolean equals(boolean[] a, boolean[] a2)
public static boolean equals(byte[] a, byte[] a2)
public static boolean equals(char[] a, char[] a2)
public static boolean equals(double[] a, double[] a2)
public static boolean equals(float[] a, float[] a2)
public static boolean equals(int[] a, int[] a2)
public static boolean equals(long[] a, long[] a2)
public static boolean equals(Object[] a, Object[] a2)
public static boolean equals(short[] a, short[] a2)
import java.util.Arrays;
public class ArrayEqualsExample {
public static void main(String[] args) {
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {1, 2, 3, 4, 5};
int[] array3 = {5, 4, 3, 2, 1};
// Compare array1 and array2
boolean isEqual = Arrays.equals(array1, array2);
System.out.println("array1 and array2 are equal: " + isEqual); // Output: true
// Compare array1 and array3
isEqual = Arrays.equals(array1, array3);
System.out.println("array1 and array3 are equal: " + isEqual); // Output: false
}
}
import java.util.Arrays;
public class ArrayEqualsExample {
public static void main(String[] args) {
String[] array1 = {"apple", "banana", "cherry"};
String[] array2 = {"apple", "banana", "cherry"};
String[] array3 = {"apple", "banana", "date"};
// Compare array1 and array2
boolean isEqual = Arrays.equals(array1, array2);
System.out.println("array1 and array2 are equal: " + isEqual); // Output: true
// Compare array1 and array3
isEqual = Arrays.equals(array1, array3);
System.out.println("array1 and array3 are equal: " + isEqual); // Output: false
}
}
Arrays.equals()
performs a linear search and compares each element. Its time complexity is O(n) where n is the number of elements in the array.Arrays.equals()
does not compare nested arrays element-wise. Instead, you should use Arrays.deepEquals()
for deep comparison of nested arrays.import java.util.Arrays;
public class ArrayDeepEqualsExample {
public static void main(String[] args) {
int[][] array1 = {{1, 2, 3}, {4, 5, 6}};
int[][] array2 = {{1, 2, 3}, {4, 5, 6}};
int[][] array3 = {{1, 2, 3}, {6, 5, 4}};
// Compare array1 and array2
boolean isEqual = Arrays.deepEquals(array1, array2);
System.out.println("array1 and array2 are equal: " + isEqual); // Output: true
// Compare array1 and array3
isEqual = Arrays.deepEquals(array1, array3);
System.out.println("array1 and array3 are equal: " + isEqual); // Output: false
}
}
In summary, Arrays.equals()
is a useful method for comparing arrays element-by-element, and it’s essential to use the appropriate variant or method (like Arrays.deepEquals()
) for your specific use case.