Arrays.mismatch()

The Java programming language offers a range of built-in methods to assist in comparing and manipulating arrays. One such method is the Arrays.mismatch() method, introduced in Java 9. The mismatch() method is used to compare two arrays and return the index of the first unmatched element. If the two arrays are identical, it returns -1.

The Arrays.mismatch() method comes in four different versions to cater to different data types and scenarios. The first version takes two arrays of primitive types, while the second version takes two sub-arrays within two arrays of primitive types. The third version takes two arrays of non-primitive types or where a user-defined Comparator case of generic type is needed to be considered. The fourth version is similar to the third, but it takes two sub-arrays within two arrays of non-primitive types or where a user-defined Comparator case of generic type is needed to be considered.

Let's take a look at some examples of how to use the Arrays.mismatch() method. In the first example, we will demonstrate how to use the method to compare two arrays of primitive types:

less
int a[] = {10, 20, 30}; int b[] = {10, 20, 30}; int c[] = {10, 20, 40, 30}; System.out.println(Arrays.mismatch(a, b)); // -1 System.out.println(Arrays.mismatch(a, c)); // 2 System.out.println(Arrays.mismatch(a, 0, 2, b, 0, 2)); // -1

In this example, we create three arrays of primitive types: a, b, and c. We then use the mismatch() method to compare the arrays. In the first call, we compare arrays a and b and get a result of -1, indicating that the two arrays are identical. In the second call, we compare arrays a and c and get a result of 2, indicating that the third element in the two arrays is different. In the third call, we compare a sub-array of a and a sub-array of b and get a result of -1, indicating that the two sub-arrays are identical.

In the second example, we will demonstrate how to use the Arrays.mismatch() method to compare two arrays of non-primitive types using a user-defined Comparator case:

rust
String a[] = {"GeeksforGeeks", "IDE"}; String b[] = {"geeksforgeeks", "Courses"}; System.out.println(Arrays.mismatch(a, b, String::compareToIgnoreCase)); // 1

In this example, we create two arrays of non-primitive types: a and b. We then use the mismatch() method to compare the arrays, passing a Comparator case that ignores the cases of the strings. We get a result of 1, indicating that the second element in the two arrays is different.

One practical application of the Arrays.mismatch() method is finding the longest common prefix in two arrays. To do this, we can use the method to find the index of the first unmatched element between the two arrays and then extract the common prefix up to that index. Here is an example:

javascript
String[] arr1 = {"apple", "app", "april", "apartment"}; String[] arr2 = {"apple", "application", "april", "apartment"}; int mismatchIndex = Arrays.mismatch(arr1, arr2); String longestCommonPrefix = arr1[0].substring(0, mismatchIndex); System.out.println(longestCommonPrefix); // "ap"

In this example, we create two arrays of strings: arr1 and arr2. We then use the mismatch() method to find the index of the first unmatched element between the two arrays. In order to find the longest common prefix in the two arrays, we can use the index of the first unmatched element to extract the common prefix.

Let's see how this can be done:

typescript
import java.util.Arrays; public class Main { public static void main(String[] args) { String[] arr1 = {"apple", "apricot", "banana", "blueberry"}; String[] arr2 = {"apple", "apricot", "berry", "blueberry"}; // Find the index of the first unmatched element int mismatchIndex = Arrays.mismatch(arr1, arr2); // Extract the common prefix String[] commonPrefix = Arrays.copyOfRange(arr1, 0, mismatchIndex); // Print the common prefix System.out.println("The longest common prefix is: " + String.join(", ", commonPrefix)); } }

In this example, the mismatch() method will return the index 2, which is the index of the first unmatched element in the two arrays. We can then use the copyOfRange() method to extract the common prefix, which is an array of strings containing the elements from the beginning of the array up to the mismatch index. Finally, we use the join() method to convert the array of strings into a single string separated by commas and print it out.

This technique can be useful in a variety of applications, such as finding common prefixes in lists of file names or identifying shared characteristics in sets of data. The mismatch() method is a powerful tool for quickly identifying differences between arrays, and can be especially useful in cases where large amounts of data need to be compared efficiently.

Previous Post Next Post