Comparing Arrays – Collections: Part II
By Jaime Williams / March 23, 2022 / No Comments / Composing Stream Pipelines, Oracle Certification Exam, Replacing Elements in Collections
Comparing Arrays
The java.util.Arrays class provides a rich set of static methods for comparing arrays of primitive data types and objects. In this section we only consider the basic methods for array equality, array comparison, and array mismatch. We encourage the curious reader to explore the Arrays class API for more flexible comparison of arrays.
Array Equality
The Arrays.equals() static method can be used to determine if two arrays are equal.
boolean equals(
type
[] a,
type
[] b)
The permitted type for elements includes all primitive types (boolean, byte, char, double, float, int, long, short) and Object.
Two arrays are considered equal if they contain the same elements in the same order—that is, they have the same length and corresponding pairs of elements are equal. Two array references are also considered equal if both are null.
Two primitive values v1 and v2 are considered equal if new Wrapper-Class(v1).equals(new WrapperClass(v2)),where WrapperClass is the wrapper class corresponding to their primitive data type.
Two objects obj1 and obj2 are considered equal if Objects.equals(obj1, obj2).
Given two arrays fruitBasketA and fruitBasketB of strings, shown below graphically, we can conclude that they are equal, since they have the same length and corresponding pairs of fruits are equal.
Index 0 1 2 3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketB ==> [oranges, apples, plums, kiwi]
Equals: true
However, the arrays fruitBasketA and fruitBasketC below would not be considered equal. Although they have the same length and the same fruits, their corresponding pairs of fruits are not equal. The first index where this occurs is index 2.
Index 0 1 2 3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketC ==> [oranges, apples, kiwi, plums]
Equals: false
Obviously, the two arrays fruitBasketA and fruitBasketE that have different lengths are not equal, as we can see below:
Index 0 1 2 3
fruitBasketA ==> [oranges, apples, plums, kiwi]
fruitBasketE ==> [oranges, apples]
Equals: false
The following code demonstrates the examples provided above, where System.out is statically imported:
String[] fruitBasketA = { “oranges”, “apples”, “plums”, “kiwi” };
String[] fruitBasketB = { “oranges”, “apples”, “plums”, “kiwi” };
String[] fruitBasketC = { “oranges”, “apples”, “kiwi”, “plums” };
String[] fruitBasketE = { “oranges”, “apples” };
…
out.println(“Equals: ” + Arrays.equals(fruitBasketA, fruitBasketB)); // true
out.println(“Equals: ” + Arrays.equals(fruitBasketA, fruitBasketC)); // false
out.println(“Equals: ” + Arrays.equals(fruitBasketA, fruitBasketE)); // false
According to the equals() method, two null arrays are equal. However, the first statement below will not compile. The compiler issues an error, as the method call matches many overloaded methods named equals in the Arrays API. A cast is necessary to disambiguate the method call, as shown in the second statement.
out.println(Arrays.equals(null, null)); // Ambiguous method call.
// Compile-time error!
out.println(Arrays.equals((String[]) null, null)); // true