Java Arrays mismatch(T[] a, T[] b, Comparator<? super T> cmp) Method



Description

The Java Arrays mismatch(T[] a, T[] b) method finds and returns the first mismatch between two Object arrays. Objects are compared using the provided comparator. In case of no mismatch, -1 is returned. In case of common prefix, length of the common index is returned. In case one array is a proper prefix of other array, smaller array's length is returned.

Declaration

Following is the declaration for java.util.Arrays.mismatch(T[] a, T[] b, Comparator<? super T> cmp) method

 public static int mismatch(T[] a, T[] b) 

Parameters

  • a − This is the first array to be tested for mismatch.

  • b − This is the second array to be tested for mismatch.

  • cmp − This is the comparator to compare array elements.

Return Value

This method the index of the first mismatch between the two arrays, otherwise -1.

Exception

  • NullPointerException − if either array or the comparator is null.

Java Arrays mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex ,Comparator<? super T> cmp) Method

Description

The Java Arrays mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method finds and returns the first mismatch between two T arrays in the given ranges. Objects are compared using the provided comparator. In case of any array null, a NullPointerException is thrown.

Declaration

Following is the declaration for java.util.Arrays.mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) method

 public static int mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) 

Parameters

  • a − This is the first array to be tested for mismatch.

  • aFromIndex − This is the index of the first element (inclusive) of first array to be tested for mismatch.

  • aToIndex − This is the index of the last element (exclusive) of first array to be tested for mismatch.

  • b − This is the second array to be tested for mismatch.

  • bFromIndex − This is the index of the first element (inclusive) of second array to be tested for mismatch.

  • bToIndex − This is the index of the last element (exclusive) of second array tto be tested for mismatch.

  • cmp − This is the comparator to compare array elements.

Return Value

This method returns the relative index of the first mismatch between the two arrays over the specified ranges, otherwise -1.

Exception

  • IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex

  • ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length

  • NullPointerException − if either array or the comparator is null

Checking Arrays of Objects for Mismatch Example

The following example shows the usage of Java Arrays mismatch(T[], T[], Comparator) method. First, we've created two arrays of same Student objects, and checked them using mismatch() method. Result is printed.

 package com.tutorialspoint; import java.util.Arrays; import java.util.Comparator; public class ArrayDemo { public static void main(String[] args) { // initialize first students array Student array1[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") }; // initialize second students array Student array2[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") }; RollNoComparator comparator = new RollNoComparator(); int result = Arrays.mismatch(array1, array2, comparator); if(result == -1) { System.out.println("No mismatch. Arrays are same."); } else { System.out.println("First mismatch is at index: " + result); } } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } } 

Output

Let us compile and run the above program, this will produce the following result −

 No mismatch. Arrays are same. 

Checking Sub-Arrays of Objects for Mismatch Example

The following example shows the usage of Java Arrays mismatch(T[],int,int,T[],int,int,Comparator) method. First, we've created two arrays of different Student objects, and checked their sub-arrays using mismatch() method. Result is printed.

 package com.tutorialspoint; import java.util.Arrays; import java.util.Comparator; public class ArrayDemo { public static void main(String[] args) { // initialize first students array Student array1[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") }; // initialize second students array Student array2[] = { new Student(1, "Julie"), new Student(4, "Jene"), new Student(2, "Robert") }; RollNoComparator comparator = new RollNoComparator(); int result = Arrays.mismatch(array1, 0, 2, array2, 0, 2, comparator); if(result == -1) { System.out.println("No mismatch. Arrays are same."); } else { System.out.println("First mismatch is at index: " + result); } } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } } 

Output

Let us compile and run the above program, this will produce the following result −

 First mismatch is at index: 1 

Checking Sub-Arrays of Objects for Mismatch Example

The following example shows the usage of Java Arrays mismatch(T[],int,int,T[],int,int,Comparator) method. First, we've created two arrays of different Student objects, and checked their sub-arrays using mismatch() method. Result is printed.

 package com.tutorialspoint; import java.util.Arrays; import java.util.Comparator; public class ArrayDemo { public static void main(String[] args) { // initialize first students array Student array1[] = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") }; // initialize second students array Student array2[] = { new Student(1, "Julie"), new Student(4, "Jene"), new Student(2, "Robert") }; RollNoComparator comparator = new RollNoComparator(); int result = Arrays.mismatch(array1, 2, 2, array2, 2, 2); if(result == -1) { System.out.println("No mismatch. Arrays are same."); } else { System.out.println("First mismatch is at index: " + result); } } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class RollNoComparator implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return o1.getRollNo()-o2.getRollNo(); } } 

Output

Let us compile and run the above program, this will produce the following result −

 No mismatch. Arrays are same. 
java_util_arrays.htm
Advertisements