Arrays.equals()

The Arrays.equals() method is a useful tool for checking whether two arrays are equal or not. In Java, two arrays are considered equal if they contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Additionally, two array references are considered equal if both are null.

Prior to Java 9, there was only one version of the Arrays.equals() method that took two parameters:

java
public static boolean equals(a[], b[])

This version of the method takes in two arrays and returns true if they are equal, and false otherwise.

Starting in Java 9, a second version of the Arrays.equals() method was introduced that takes six parameters:

java
public static boolean equals(a[], aStart, aEnd, b[], bStart, bEnd)

This version of the method is used to compare a subarray within one array to a subarray within another array. It takes in the two arrays, as well as the start and end indices of the subarrays to be compared.

Here are a few examples of how you can use the Arrays.equals() method:

Example 1:

java
import java.util.Arrays; public class GfG { public static void main(String[] args) { // Let us create different integers arrays int[] a = {10, 15, 20}; int[] b = {10, 15, 20}; int[] c = {15, 10, 20}; // Compares the arrays a and b if (Arrays.equals(a, b)) System.out.println("Yes"); else System.out.println("No"); // Compares the arrays a and c if (Arrays.equals(a, c)) System.out.println("Yes"); else System.out.println("No"); } }

Output:

yaml
Yes No

Example 2:

java
import java.util.Arrays; public class GfG { public static void main(String[] args) { // Let us create different arrays // of Integer wrapper class Integer a[] = {10, 15, null, 30}; Integer b[] = {10, 15, null, 30}; // Compares the arrays a and b if (Arrays.equals(a, b)) System.out.println("Yes"); else System.out.println("No"); } }

Output:

yaml
Yes

Example 3:

java
import java.util.Arrays; public class GfG { public static void main(String[] args) { // Let us create different arrays // of Integer wrapper class Integer a[] = {10, 15, 30, null}; Integer b[] = {10, 15, null, 30}; // Compares the arrays a and b if (Arrays.equals(a, b)) System.out.println("Yes"); else System.out.println("No"); } }

Output:

yaml
No

Example 4:

java
import java.util.Arrays; public class GfG { public static void main(String[] args) { // Let us create different arrays // of Integer wrapper class Integer a[] = {10, 20, 30, 40, 50};
Integer b[] = {30, 40, 50};
System.out.println("Array a[] : " + Arrays.toString(a)); System.out.println("Array b[] : " + Arrays.toString(b)); // To find a common element from arrays // using retainAll() method ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(a)); list.retainAll(Arrays.asList(b)); System.out.println("Common Elements : " + list); }}


In the above code, we are creating two arrays "a" and "b" of Integer wrapper class. The arrays are then converted into ArrayLists using the Arrays.asList() method. The retainAll() method is then used on the first list to retain only those elements that are also present in the second list.

The resulting list will contain only the common elements present in both arrays. Finally, the list is printed using the System.out.println() method.

This code is a simple example of how to find common elements in two arrays using the retainAll() method in Java. It can be used as a starting point for more complex operations involving arrays and lists.


Previous Post Next Post