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:
1 2 3 |
public static boolean equals(int[] a, int[] a2) public static boolean equals(char[] a, char[] a2) public static boolean equals(byte[] a, byte[] a2) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) public static boolean equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) |
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:
1 2 3 4 5 6 7 8 9 10 11 |
public void arrayEqualsTest() { int[] existRows = {0, 1, 2, 3, 4, 5}; int[] newRows = {3, 4, 5, 1, 2, 0}; assertFalse(Arrays.equals(existRows, newRows)); assertTrue(Arrays.equals(existRows, 1, 3, newRows, 3, 5)); assertTrue(Arrays.equals(existRows, 3, 5, newRows, 0, 2)); } |
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:
1 |
public static int compare(int[] a, int[] b) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Test public void compareArraysTest() { int[] tomMarks = {5, 6, 7, 8, 9, 10}; int[] aliceMarks = {5, 6, 7, 8, 9, 10}; int[] daisyMarks = {5, 6, 7, 10, 9, 10}; int[] maryMarks = {5, 6, 7, 8}; assertEquals(0, Arrays.compare(tomMarks, aliceMarks)); assertEquals(-1, Arrays.compare(tomMarks, daisyMarks)); assertEquals(2, Arrays.compare(tomMarks, maryMarks)); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static int compare(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) public static int compare(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) |
The method compares two int arrays lexicographically over the specified ranges, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public void compareSliceArraysTest() { int[] tomMarks = {5, 6, 7, 8, 9, 10}; int[] daisyMarks = {5, 6, 7, 10, 9, 10}; int[] maryMarks = {5, 6, 7, 8}; assertEquals(0, Arrays.compare(tomMarks, 0, 3, daisyMarks, 0, 3)); assertEquals(0, Arrays.compare(tomMarks, 0, 4, maryMarks, 0, maryMarks.length)); assertEquals(1, Arrays.compare(daisyMarks, 0, 4, maryMarks, 0, maryMarks.length)); } |
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:
1 |
public static int mismatch(int[] a, int[] b) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static int mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) public static int mismatch(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Test public void mismatchArraysTest() { int[] a = {1, 2, 3, 4, 5}; int[] b = {1, 2, 3, 4, 5}; int[] c = {1, 2, 4, 4, 5, 6}; assertEquals(-1, Arrays.mismatch(a, b)); assertEquals(2, Arrays.mismatch(a, c)); assertEquals(-1, Arrays.mismatch(a, 0, 2, c, 0, 2)); assertEquals(2, Arrays.mismatch(a, 0, 3, c, 0, 3)); //mismatch return relative index assertEquals(0, Arrays.mismatch(a, 2, a.length, c, 2, 5)); } |
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:
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
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
Java 9 HTTP 2 Client API Example