{{announcement.body}}
{{announcement.title}}

# Checking Whether Two Arrays Are Equal or Contain Mismatches

DZone 's Guide to

# 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.

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

## 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:

Java

x

1
`int[] integers1 = {3, 4, 5, 6, 1, 5};`
2
`int[] integers2 = {3, 4, 5, 6, 1, 5};`
3
`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:

Java

`xxxxxxxxxx`
1

1
`boolean i12 = Arrays.equals(integers1, integers2); // true`
2
`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:

Java

`xxxxxxxxxx`
1

1
`boolean equals(int[] a, int aFromIndex, `
2
`               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` ):

Java

Now, let's assume three arrays of  `Melon` :

Java

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:

Java

And, in an explicit range, we can use the following method:

Java

`xxxxxxxxxx`
1

1
` boolean equals(Object[] a, int aFromIndex, `
2
`                int aToIndex, Object[] b, int bFromIndex, int bToIndex)`

For example:

Java

`xxxxxxxxxx`
1

1
`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` :

Java

`xxxxxxxxxx`
1

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

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

Java

`xxxxxxxxxx`
1

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

And, in an explicit range, using:

Java

`xxxxxxxxxx`
1

1
`Comparator, <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b,`
2
`                               int bFromIndex, int bToIndex, Comparator<? super T> cmp)`

We have the following:

Java

`xxxxxxxxxx`
1

1
`// true`
2
`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:

Java

`xxxxxxxxxx`
1

1
`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:

Java

`xxxxxxxxxx`
1

1
`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:

Java

`xxxxxxxxxx`
1

1
`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` :

Java

`xxxxxxxxxx`
1

1
`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` :

Java

`xxxxxxxxxx`
1
10

1
`// range [1, 2), return -1`
2
`int mms13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2);`
3
` `
4
`// Comparator by melon's weights, return -1`
5
`int mmw13 = Arrays.mismatch(melons1, melons3, byWeight);`
6
`  `
7
`// Comparator by melon's types, return 0`
8
`int mmt13 = Arrays.mismatch(melons1, melons3, byType);`
9
`  `
10
`// range [1,2) and Comparator by melon's types, return -1`
11
`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.

A Guide to Arrays in Java

How to Convert Between List and Array in Java

Topics:
java ,arrays ,comparison ,jdk ,equal arrays

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.