Comparing two tables in Java
In Java, you can compare two arrays by comparing each element of the array. The Arrays provides methods that can be useful for doing the comparison of the two arrays in Java.
We'll learn how to compare two arrays in Java using the Arrays.equals() and Arrays.deepEquals()with examples.
Two arrays are equal if they have:
- The same type.
- The same number of elements.
- Each pair of elements in both arrays are equal.
- The order of the elements must be the same.
The Arrays.equals() method
The Java class Arrays provides the method equals() to compare two tables. Its principle is to compare the elements of the two arrays by doing the traverse.
public static boolean equals(int[] a1, int[] a2)
The method equals returns true if the elements of the array are equal otherwise false.
Example:
import java.util.Arrays;Runtime:
public class equals
{
public static void main (String[] args)
{
// tables to compare
int[] array1 = new int[] {1, 2, 3, 4, 5};
int[] array2 = new int[] {1, 2, 3, 4, 5};
int[] array3 = new int[] {1, 2, 2, 4, 5};
//compare arrays array1 and array2 with the equals
if method(Arrays.equals(array1, array2))
System.out.println("Arrays are equal");
else
System.out.println("Arrays are not equal.");
// compare array1 with array3
if (Arrays.equals(array1, array3))
System.out.println("Arrays are equal");
else
System.out.println("Arrays are not equal.");
}
}
Arrays equal
Arrays are not equal.
The Arrays.equals() compares the elements of the two arrays.
Note: if the two arrays are contained in different objects but have the same values, then they are not equal as shown in the example below.
public class comparisonArrays2
{
public static void main (String[] args)
{
// tables to compare
String[] array1 = new String[] {"a", "b", "c", "d"};
String[] array2 = new String[] {"a", "b", "c", "d"};
Object[] arrayObj1 = {array1};
Object[] arrayObj2 = {array2};
// compare arrayObj1 and arrayObj2
if (Arrays.equals(arrayObj1, arrayObj2))
System.out.println("Arrays are equal");
else
System.out.println("Arrays are not equal.");
}
}
Execution:
Arrays are not equal.
We can see that Arrays.equals() can't make a deeper comparison and to solve this you need to use the deepEquals().
The Arrays.deepEquals()
The Arrays provides another more efficient method for comparing the contents of arrays in a more in-depth way as the name suggests.
public static boolean deepEquals(Object[] t1, Object[] t2)
The deepEquals parse both arrays t1 and T2 and compares them. after the deep analysis, it returns ture if the two are equal otherwise false.
Two arrays t1 et t2 of type Object are equal if they have at least one of the following properties:
- t1 = t2
- equals(t2) returns true.
- If t1 and t2 have the same object reference type, the method deepEquals(t1,t2) returns true.
- If t1 and t2 have the same primitive type, the method equals(t1,t2) returns true.
Example:
public class comparisonArrays3
{
public static void main (String[] args)
{
// tables to compare
String[] array1 = new String[] {"java", "php", "javascript", "c++"};
String[] array2 = new String[] {"java", "php", "javascript", "c++"};
Object[] t1 = {array1};
Object[] t2 = {array2};
// compare arrayObj1 and arrayObj2
if (Arrays.deepEquals(t1, t2))
System.out.println("Arrays are equal");
else
System.out.println("Arrays are not equal.");
}
}
Runtime:
Arrays are equal
References: