How To Compare Arrays In Java 9

In this tutorial, we’re going to get through new methods that have been added to the java.util.Arrays class since Java 9, which enables us to compare arrays and slices of arrays in Java efficiently.

1. Compare Arrays Equality In Java 9

Firstly, let’s take a look at the Arrays.equals methods, which were existed since prior versions of Java, enable us to compare arrays for equality in Java:

Those are just some typical forms of the Arrays.equals method; the class provides us other overloaded methods which can be used to compare arrays of boolean, double and other types in Java.

Next, let’s see some methods have been added to the java.util.Arrays class since Java 9, which enables us to compare slices of arrays:

The first method returns true if the two specified arrays of ints, over the specified ranges, are equal to one another, and the second method works the same for the arrays of chars.

Notice that there are also other overloaded methods works the same for all remaining primitives types and other types as well in Java.

Now, let’s get to some examples which we use above methods to compare slices of arrays for equality in Java 9:

2. Compare Arrays Lexicographically In Java 9

From Java 9, we can compare arrays lexicographically by using the Arrays.compare methods which can be applied to arrays of all primitive types in Java. Following is a form of the method which enables us to compare arrays of integers lexicographically:

Notice that there are other overloaded methods for all remaining primitive types and other types as well in Java. Now, let’s get to some examples which we use the Arrays.compare to compare arrays of ints in Java:

In addition, Java 9 also added some more overloaded methods of the Arrays.compare method, which enables us to compare slices of arrays lexicographically in Java, for example:

The method compares two int arrays lexicographically over the specified ranges, for example:

3. Finds The Index Of The First Mismatch Between Two Arrays

Another method we will get through in this tutorial is the Arrays.mismatch() method, which finds and returns the index of the first mismatch between two arrays of all primitive types in Java. Firstly, let’s take a look at the syntax of Arrays.mismatch() method which accepts two arrays of integers as parameters:

The above mismatch() method finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found. The index will be in the range of 0 (inclusive) up to the length (inclusive) of the smaller array.

Besides, there are other overloaded methods of the Arrays.mismatch() methods which enables us to finds and returns the index of the first mismatch between two slices of arrays, for example:

The method finds and returns the relative index of the first mismatch between two int arrays over the specified ranges, otherwise, return -1 if no mismatch is found. The index will be in the range of 0 (inclusive) up to the length (inclusive) of the smaller range.

Next, let’s get to some examples of above mismatch() methods:

4. Conclusions

The tutorial has illustrated us how to compare arrays in Java 9 by different related methods such as equal(), compare() and mismatch() that have been added to the java.util.Arrays class from JDK 9. Notice that the class includes other overloaded methods that can be used for comparing arrays and slices of arrays of different types in Java. For more detail, please visit the official website of the class.

The sample code presented in this tutorial is available on Github. It’s a Maven based project; it’s easy to be imported in IDE such as Eclipse, IntelliJ, etc.

Below are other related tutorials for your references:

Java 9 Tutorial

Install Oracle Java 9 on CentOS, RHEL 7

Install Oracle Java 9 on Ubuntu 16.04 LTS (Xenial Xerus)

Set Up Eclipse, IntelliJ And NetBeans For Java 9

Java 9 Example With Maven And JUnit 5

Java 9 JShell Cheat Sheet

Create Immutable Lists In Java 9 By Static Factory Methods

Private Interface Methods In Java 9

Using The InputStream.transferTo() To Copy Streams In Java 9

Java 9 – New Methods Of The Optional Class

Streams API Updates In Java 9

Java 9 HTTP 2 Client API Example