How to compare elements of two arrays in Java 8

Harsh Jain

Two arrays contain the same elements when the number of elements as well as the values of the elements are identical in both arrays.

Note: We need to identify the similarity in the elements themselves, not the similarity in the order of elements of the two arrays.

Example

Let’s understand with the help of an example.

Suppose that the first array contains [1, 3, 4, 5, 7] and the second array contains [1, 7, 4, 5, 3]. The two arrays contain the same elements since their size is the same and the data elements are also identical.

Another example could be that the first array contains [1, 3, 4, 5, 7] and the second array contains [1, 6, 2, 5, 3]. The two arrays do not contain the same elements since even though their size is the same, some of the elements differ.

Solution approach

We will be using two methods: Arrays.sort() and Arrays.equals() from the java.util package to solve this problem.

  • The Arrays.sort() method sorts the elements of the array in increasing order. This method will accept the array which needs to be sorted and return the sorted array.

  • The Arrays.equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

Code

The code for the algorithm above is provided.

import java.util.*; class Main { public static void main(String[] args) { Object[] a = new Object[] {1, 5, 7, 9}; Object[] b = new Object[] {9, 5, 1, 7}; Object[] c = new Object[] {1, 6, 7, 5}; Arrays.sort(a); Arrays.sort(b); Arrays.sort(c); boolean returnVal1 = Arrays.equals(a,b); System.out.println("Array a and b equal?: " + returnVal1); boolean returnVal2 = Arrays.equals(b,c); System.out.println("Array b and c equal?: " + returnVal2); } }

Check whether all the elements in two arrays are same or not in Java

RELATED TAGS

array

java

communitycreator

In Java, we can compare two arrays by comparing each element of the array. Java Arrays class provides two predefined methods that is used to compare two arrays in Java.

In this section, we will learn how to compare two Arrays using Arrays.equals() method and Arrays.deepEquals() method. Along with this, we will also learn how to perform a deep comparison between the two arrays with proper examples.

Two arrays are equal if:

  • They are of the same type.
  • They have an equal number of elements.
  • Corresponding pairs of elements in both arrays must be equal.
  • The order of elements must be the same.
  • Two array references are equal if they are null.

Before moving to the topic, first, consider the following example and guess the output.

ArrayEqualsExample1.java

public class ArrayEqualsExample1 { public static void main (String[] args) { //defining arrays to be compare int[] a1 = new int[] {1, 2, 3, 4, 5, 6, 7, 8}; int[] a2 = new int[] {1, 2, 3, 4, 5, 6, 7, 8}; //comparing references using == operator //works the same as a1.equals(a2) if (a1 == a2) System.out.println("Arrays are equal."); else System.out.println("Arrays are not equal."); } }

Output:

In the above example, a1 and a2 are the two references of two different objects. When we compare two reference variables, we get the output Arrays are not equal, while both the arrays are of equal length, and having the same elements. We are not getting the desired output because the equals (==) operator compare them as an object.

Now, we have only an option to compare two arrays, i.e. compare the content (elements) of the array. Let's see how to compare array contents.

It will be good if we compare the elements one by one. To compare the content of the array Java Arrays class provides the following two methods to compare two arrays:

  • equals() Method
  • deepEquals() Method

Arrays.equals() Method

Java Arrays class provides the equals() method to compare two arrays. It iterates over each value of an array and compares the elements using the equals() method.

Syntax:

public static boolean equals(int[] a1, int[] a2)

It parses two arrays a1 and a2 that are to compare. The method returns true if arrays are equal, else returns false. The Arrays class has a list of overloaded equals() method for different primitive types and one for an Object type.

Note: While using the array of objects, don't forget to override the equals() method. Otherwise, we will get the output returned by the equals() method of the Object class.

ArrayEqualsExample2.java

import java.util.Arrays; public class ArrayEqualsExample2 { public static void main (String[] args) { //defining array to compare int[] array1 = new int[] {'a', 'b', 'c', 'd', 'e'}; int[] array2 = new int[] {'a', 'b', 'c', 'd', 'e'}; //comparing two arrays using equals() method if (Arrays.equals(array1, array2)) System.out.println("Arrays are equal."); else System.out.println("Arrays are not equal."); } }

Output:

Let's see another example.

ArrayEqualsExample3.java

import java.util.Arrays; public class ArrayEqualsExample3 { public static void main (String[] args) { //defining arrays to compare int[] array1 = new int [] {21, 32, 36, 47}; int[] array2 = new int [] {11, 22, 33, 44}; int[] array3 = new int [] {21, 32, 36, 47}; System.out.println("Are array1 and array2 equal?" + Arrays.equals(array1, array2)); System.out.println("Are array1 and array3 equal?" + Arrays.equals(array1, array3)); } }

Output:

Are array1 and array2 equal? false Are array1 and array3 equal? true

We see that the Arrays.equals() method compares the elements of the array. Here, a question arises that what if an array has nested array or some other references which refer to different object but have the same values.

Let's understand it through the following example.

ArrayEqualsExample4.java

import java.util.Arrays; public class ArrayEqualsExample4 { public static void main (String[] args) { //defining array to compare String[] inarray1 = new String[] {"mango", "grapes", "plum", "watermelon", "apple"}; String[] inarray2 = new String[] {"mango", "grapes", "plum", "watermelon", "apple"}; Object[] array1 = {inarray1}; // array1 have only one element Object[] array2 = {inarray2}; // array2 also have only one element //comparing two arrays using equals() method if (Arrays.equals(array1, array2)) System.out.println("Arrays are equal."); else System.out.println("Arrays are not equal."); } }

Output:

In the above example, we see that the equals() method is not able to perform a deep comparison. To resolve this problem, the Java Arrays class provides another method deepEquals() that deeply compares the two arrays.

Arrays.deepEquals() Method

Java Arrays class provides another method deepEquals() to deeply compare the array. Here, deeply compare means it can compare two nested arrays of arbitrary depth. Two arrays object reference e1 and e2 are deeply equal if they hold any of the following condition:

  • e1=e2
  • equals(e2) returns true.
  • If e1 and e2 are both the same primitive type the overloading of the method Arrays.equals(e1, e2) returns true.
  • If e1 and e2 are both arrays of object reference types, the method Arrays.deepEquals(e1, e2) returns true.

Syntax:

public static boolean deepEquals(Object[] a1, Object[] a2)

The method parses the two arrays a1 and a2 that is to compare. It returns true if both arrays are deeply equal, else returns false.

Let's create a program and deeply compare two arrays using the deepEquals() method of the Arrays class.

ArrayEqualsExample5.java

import java.util.Arrays; public class ArrayEqualsExample5 { public static void main (String[] args) { //defining array to compare String[] inarray1 = new String[] {"mango", "grapes", "plum", "watermelon", "apple"}; String[] inarray2 = new String[] {"mango", "grapes", "plum", "watermelon", "apple"}; Object[] array1 = {inarray1}; // array1 have only one element Object[] array2 = {inarray2}; // array2 also have only one element //comparing two arrays using equals() method if (Arrays.deepEquals(array1, array2)) System.out.println("Arrays are equal."); else System.out.println("Arrays are not equal."); } }

Output:

Next TopicHow to Convert String to JSON Object in Java

Postingan terbaru

LIHAT SEMUA