Checking Whether Two Arrays Are Equal or Contain Mismatches
Use this tutorial to determine whether two arrays are equal or mismatched using the JDK API.
Join the DZone community and get the full member experience.
Join For FreeChecking 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};
You may also enjoy: JDK 12's Files.mismatch Method
Now, let's check whether integers1
is equal to integers2
, and whether integers1
is equal to integers3
. This is very simple:
xxxxxxxxxx
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 following method:
xxxxxxxxxx
boolean equals(int[] a, int aFromIndex,
int aToIndex, int[] b, int bFromIndex, int bToIndex)
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
:
xxxxxxxxxx
public class Melon {
private final String type;
private final int weight;
public Melon(String type, int weight) {
this.type = type;
this.weight = weight;
}
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.type);
hash = 83 * hash + this.weight;
return hash;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Melon other = (Melon) obj;
if (this.weight != other.weight) {
return false;
}
if (!Objects.equals(this.type, other.type)) {
return false;
}
return true;
}
}
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:
xxxxxxxxxx
boolean m12 = Arrays.equals(melons1, melons2); // true
boolean m13 = Arrays.equals(melons1, melons3); // false
And, in an explicit range, we can use the following method:
xxxxxxxxxx
boolean equals(Object[] a, int aFromIndex,
int aToIndex, Object[] b, int bFromIndex, int bToIndex)
For example:
xxxxxxxxxx
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
:
xxxxxxxxxx
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:
xxxxxxxxxx
boolean mw13 = Arrays.equals(melons1, melons3, byWeight); // true
boolean mt13 = Arrays.equals(melons1, melons3, byType); // false
And, in an explicit range, using:
xxxxxxxxxx
Comparator, <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b,
int bFromIndex, int bToIndex, Comparator<? super T> cmp)
We have the following:
xxxxxxxxxx
// true
boolean mrt13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2, byType);
Checking Whether Two Arrays Contain Mismatches
If two arrays are equal, then a mismatch should return -1. But if two arrays are not equal, then a mismatch should return the index of the first mismatch between the two given arrays. In order to resolve this problem, we can rely on JDK 9 Arrays.mismatch()
methods.
For example, we can check for mismatches between integers1
and integers2
as follows:
xxxxxxxxxx
int mi12 = Arrays.mismatch(integers1, integers2); // -1
The result is -1, since integers1
and integers2
are equal. But if we check for integers1
and integers3
, we receive the value 5, which is the index of the first mismatch between these two:
xxxxxxxxxx
int mi13 = Arrays.mismatch(integers1, integers3); // 5
If the given arrays have different lengths and the smaller one is a prefix for the larger one, then the returned mismatch is the length of the smaller array.
For arrays of Object
, there are dedicated mismatch()
methods as well. These methods count on the equals()
contract or on the given Comparator
. We can check whether there is a mismatch between melons1
and melons2
as follows:
xxxxxxxxxx
int mm12 = Arrays.mismatch(melons1, melons2); // -1
If the mismatch occurs on the first index, then the returned value is 0. This is happening in the case of melons1
and melons3
:
xxxxxxxxxx
int mm13 = Arrays.mismatch(melons1, melons3); // 0
As in the case of Arrays.equals()
, we can check mismatches in an explicit range using a Comparator
:
xxxxxxxxxx
// range [1, 2), return -1
int mms13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2);
// Comparator by melon's weights, return -1
int mmw13 = Arrays.mismatch(melons1, melons3, byWeight);
// Comparator by melon's types, return 0
int mmt13 = Arrays.mismatch(melons1, melons3, byType);
// range [1,2) and Comparator by melon's types, return -1
int mmrt13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2, byType);
Done!
The complete source code is available on GitHub.
If you enjoyed this article, then I'm sure you will love my book, Java Coding Problems, which has an entire chapter dedicated to arrays, collections and data structures.
Further Reading
Opinions expressed by DZone contributors are their own.
Comments