Home core java Comparing arrays - Java 9 Arrays utilities

Comparing arrays – Java 9 Arrays utilities

This article is about the newly introduced JDK 9 Arrays utility class methods for comparing arrays and examples of comparing arrays.

Comparing arrays in Java 9 :

For comparing arrays and slices of arrays, more static utility methods are added to java.util.Arrays class in Java 9. Below are the list of overloaded static methods added in JDK 9 to the Arrays utility class.

  1. equals() – returns true if two arrays are equal to each other.
  2. compare() – compares two arrays lexicographically (similar to dictionary order).
  3. mismatch() – finds and returns the index of the first mismatch between two arrays.

1. Using equals() method :

equals() returns true if two arrays are equal to each other. There are two different version of different overloads for boolean, byte, char, double, float, int, long, short and Object arrays. Version one for equality check for two arrays and version two for equality check for slices of two arrays. Two arrays are considered equal if both are null.

Following are overloaded equals()s

boolean equals(int[] a, int[] b)
 
boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b,
               int bFromIndex, int bToIndex)
- Advertisement -

Example :

public class ComparingArraysEqualsDemo {
 
    public static void main(String[] args) {
        
        String[] s1 = {"Peter", "Gerhard", "Philip", "Satheesh", "Mike"};
        String[] s2 = {"Peter", "Gerhard", "Philip", "Satheesh", "Mike"};
        String[] s3 = {"Gerhard", "Mike", "Peter", "Philip", "Satheesh"};
        
        System.out.println("s1: "+ Arrays.toString(s1));
        System.out.println("s2: "+ Arrays.toString(s2));
        System.out.println("s3: "+ Arrays.toString(s3));
        
        // Comparing arrays for equalitt
        System.out.println("\nArrays.equals(s1, s2): " + Arrays.equals(s1, s2));
        System.out.println("Arrays.equals(s1, s3): " + Arrays.equals(s1, s3));
        
        // Comparing slicces of arrays for equality
        System.out.println("\nArrays.equals(s1, 0, 3, s3, 0, 3): " +
                Arrays.equals(s1, 0, 3, s3, 0, 3));
        System.out.println("Arrays.equals(s1, 0, 3, s2, 0, 3): " +
        Arrays.equals(s1, 0, 3, s2, 0, 3));
        
    }
}

Output :

s1: [Peter, Gerhard, Philip, Satheesh, Mike]
s2: [Peter, Gerhard, Philip, Satheesh, Mike]
s3: [Gerhard, Mike, Peter, Philip, Satheesh]
 
Arrays.equals(s1, s2): true
Arrays.equals(s1, s3): false
 
Arrays.equals(s1, 0, 3, s3, 0, 3): false
Arrays.equals(s1, 0, 3, s2, 0, 3): true

2. Using compare() and compareUnsigned() methods :

The group of methods compare() and compareUnsigned() compares two arrays lexicographically. There are two different version of different overloads for boolean, byte, char, double, float, int, long, short and Object arrays. Version one for equality check for two arrays and version two for equality check for slices of two arrays. The compareUnsigned() method treats the integer values as unsigned and this overloaded method applicable only for data types which can be signed.

  1. A null array is lexicographically less than a non-null array.
  2. Two arrays are considered equal if both are null.
  3. If first array and second array are equal then compare() returns zero;
  4. If the first array (or slice) is lexicographically less than the second array (or slice) returns -ve (negative value).
  5. If the first array (or slice) is lexicographically greater than the second array (or slice) returns +ve (positive value).
int compare(int[] a, int[] b)
 
int compare(int[] a, int aFromIndex, int aToIndex, int[] b, 
            int bFromIndex, int bToIndex)
            
int compare(int[] a, int[] b)
 
int compare(int[] a, int aFromIndex, int aToIndex, int[] b, 
            int bFromIndex, int bToIndex)

Example :

public class ComparingArraysCompareDemo {
 
    public static void main(String[] args) {
        
        int[] i1 = {2, 4, 6, 8, 10};
        int[] i2 = {2, 4, 6, 8, 10};
        int[] i3 = {2, 4, 12, 8, 10};
        
        System.out.println("i1: "+ Arrays.toString(i1));
        System.out.println("i2: "+ Arrays.toString(i2));
        System.out.println("i3: "+ Arrays.toString(i3));
        
        // Comparing arrays lexicographically
        System.out.println("\nArrays.compare(i1, i2): " + Arrays.compare(i1, i2));
        System.out.println("Arrays.compare(i1, i3): " + Arrays.compare(i1, i3));
        System.out.println("Arrays.compare(i3, i1): " + Arrays.compare(i3, i1));
        
        // Comparing slicces of arrays lexicographically
        System.out.println("\nArrays.compare(i1, 0, 3, i3, 0, 3): " +
                Arrays.compare(i1, 0, 3, i3, 0, 3));
        System.out.println("Arrays.compare(i1, 0, 3, i2, 0, 3): " +
                Arrays.compare(i1, 0, 3, i2, 0, 3));
        System.out.println("Arrays.compare(i3, 0, 3, i1, 0, 3): " +
                Arrays.compare(i3, 0, 3, i1, 0, 3));
        
    }
}

Output :

i1: [2, 4, 6, 8, 10]
i2: [2, 4, 6, 8, 10]
i3: [2, 4, 12, 8, 10]
 
Arrays.compare(i1, i2): 0
Arrays.compare(i1, i3): -1
Arrays.compare(i3, i1): 1
 
Arrays.compare(i1, 0, 3, i3, 0, 3): -1
Arrays.compare(i1, 0, 3, i2, 0, 3): 0
Arrays.compare(i3, 0, 3, i1, 0, 3): 1

3. Comparing arrays using mismatch() method :

The group of methods mismatch() finds and returns the index of the first mismatch between two arrays. There are different overloads for boolean, byte, char, double, float, int, long, short and Object arrays. We can also specify the start and end index in both arrays for checking the mismatch.

  1. mismatch() returns -1 if there is no mismatch.
  2. If either array is null, it throws a NullPointerException.
int mismatch(int[] a, int[] b)
 
int mismatch (int[] a, int aFromIndex, int aToIndex, int[] b, 
              int bFromIndex, int bToIndex)

Example :

public class ComparingArraysMismatchDemo {
 
    public static void main(String[] args) {
        
        int[] i1 = {2, 4, 6, 8, 10};
        int[] i2 = {2, 4, 6, 8, 10};
        int[] i3 = {2, 12, 6, 8, 10};
        
        System.out.println("i1: "+ Arrays.toString(i1));
        System.out.println("i2: "+ Arrays.toString(i2));
        System.out.println("i3: "+ Arrays.toString(i3));
        
        // Finding first mismatch
        System.out.println("\nArrays.mismatch(i1, i2): " + Arrays.mismatch(i1, i2));
        System.out.println("Arrays.mismatch(i1, i3): " + Arrays.mismatch(i1, i3));
        System.out.println("Arrays.mismatch(i3, i1): " + Arrays.mismatch(i3, i1));
        
        // Finding first mismatch in slices
        System.out.println("\nArrays.mismatch(i1, 0, 3, i3, 0, 3): " +
                Arrays.mismatch(i1, 0, 3, i3, 0, 3));
        System.out.println("Arrays.mismatch(i1, 0, 3, i2, 0, 3): " +
                Arrays.mismatch(i1, 0, 3, i2, 0, 3));
        System.out.println("Arrays.mismatch(i3, 0, 3, i1, 0, 3): " +
                Arrays.mismatch(i3, 0, 3, i1, 0, 3));
        
    }
}

Output :

i1: [2, 4, 6, 8, 10]
i2: [2, 4, 6, 8, 10]
i3: [2, 12, 6, 8, 10]
 
Arrays.mismatch(i1, i2): -1
Arrays.mismatch(i1, i3): 1
Arrays.mismatch(i3, i1): 1
 
Arrays.mismatch(i1, 0, 3, i3, 0, 3): 1
Arrays.mismatch(i1, 0, 3, i2, 0, 3): -1
Arrays.mismatch(i3, 0, 3, i1, 0, 3): 1

Also see Related Article :
Parallel array sorting using Arrays class

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles