Checking whether two arrays are equal

Checking whether two arrays are equal can be easily accomplished via the Arrays.equals() method. This flag method comes in many flavors for primitive types, Object, and generics. It also supports comparators.

Let's consider the following three arrays of integers:

int[] integers1 = {3, 4, 5, 6, 1, 5};
int[] integers2 = {3, 4, 5, 6, 1, 5};
int[] integers3 = {3, 4, 5, 6, 1, 3};

Now, let's check whether integers1 is equal to integers2, and whether integers1 is equal to integers3. This is very simple:

boolean i12 = Arrays.equals(integers1, integers2); // true
boolean i13 = Arrays.equals(integers1, integers3); // false

The preceding examples check whether two arrays are equal, but we can check whether two segments (or ranges) of the arrays are equal as well via the boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) method. So, we demarcate the segment of the first array via the range [aFromIndex, aToIndex) and the segment of the second array via the range [bFromIndex, bToIndex):

// true
boolean is13 = Arrays.equals(integers1, 1, 4, integers3, 1, 4);

Now, let's assume three arrays of Melon:

public class Melon {

private final String type;
private final int weight;

public Melon(String type, int weight) {
this.type = type;
this.weight = weight;
}

// getters, equals() and hashCode() omitted for brevity
}

Melon[] melons1 = {
new Melon("Horned", 1500), new Melon("Gac", 1000)
};

Melon[] melons2 = {
new Melon("Horned", 1500), new Melon("Gac", 1000)
};

Melon[] melons3 = {
new Melon("Hami", 1500), new Melon("Gac", 1000)
};

Two arrays of Object are considered equal based on the equals() contract, or based on the specified Comparator. We can easily check whether melons1 is equal to melons2, and whether melons1 is equal to melons3 as follows:

boolean m12 = Arrays.equals(melons1, melons2); // true
boolean m13 = Arrays.equals(melons1, melons3); // false

And, in an explicit range, use boolean equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex):

boolean ms13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2); // false

While these examples rely on the Melon.equals() implementation, the following two examples rely on the following two Comparator:

Comparator<Melon> byType = Comparator.comparing(Melon::getType);
Comparator<Melon> byWeight = Comparator.comparing(Melon::getWeight);

Using the boolean equals(T[] a, T[] a2, Comparator<? super T> cmp), we have the following:

boolean mw13 = Arrays.equals(melons1, melons3, byWeight); // true
boolean mt13 = Arrays.equals(melons1, melons3, byType); // false

And, in an explicit range, using Comparator, <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp), we have the following:

// true
boolean mrt13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2, byType);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset