Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, December 20, 2021

How To Compare Two Arrays In Java and New Java 8 API

1. Overview

In this article, you'll learn how to compare two arrays in java and how to work with comparing the contents of arrays using the deep level technique.

Comparing arrays means the first array type should be the same and contents also should be the same in both arrays.

All the examples are prepared with Arrays.equals() and Arrays.deepEquals() methods.

How To Compare Two Arrays In Java and New Java 8 API


2. Comparing Arrays Using Arrays.equals()


Arrays.equals() method does compare the contents of two given arrays. This method returns true if all values are the same in two arrays and returns false if contents are not the same.

Any object type arrays can be passed to this equals() method such as boolean, int, double and all primitive or wrapper arrays.

First, let us try with a simple example.
package com.javaprogramto.arrays.compare;

import java.util.Arrays;

public class ArraysCompare {

	public static void main(String[] args) {

		// Creating integer array 1
		int[] intArray1 = new int[] { 1, 2, 3, 4, 5 };

		// Creating integer array 2
		int[] intArray2 = new int[] { 1, 2, 3, 4, 5 };

		// comparing arrays with == operator
		System.out.println("Array comparision with == operator");
		if (intArray1 == intArray2) {
			System.out.println("int array 1 and array 2 are equal ");
		} else {
			System.out.println("int array 1 and array 2 are not equal ");
		}

		// Comparing arrays with Arrays.equals() method
		boolean isSame = Arrays.equals(intArray1, intArray2);
		System.out.println("\nArray comparision with Arrays.equals(intArray1, intArray2)");
		if (isSame) {
			System.out.println("int array 1 and array 2 are same");
		} else {
			System.out.println("int array 1 and array 2 are not same");
		}

	}

}

Output:
Array comparision with == operator
int array 1 and array 2 are not equal 

Array comparision with Arrays.equals(intArray1, intArray2)
int array 1 and array 2 are same
In the above example, first created two arrays with the same values. And next, two arrays are compared with == operator and it returned false because it does compare the references rather than values (even though values are the same). So, it executed the else block.

The below code returns true with == operator.
int[] intArray1 = new int[] { 1, 2, 3, 4, 5 };
int[] intArray2 = intArray1;
Next, used the method Arrays.equals() method with two integers arrays as arguments. This method compared each and every value in both arrays and returned true because these two are identical. Printed the output to console from if condition.

Like this, you can pass any type of array arguemnt to equals() method but two arrays should be the same type.
package com.javaprogramto.arrays.compare;

import java.util.Arrays;

public class ArraysCompareAnyType {

	public static void main(String[] args) {

		// Creating double array 1
		double[] doubleArray1 = new double[] { 1, 2, 3, 4, 5 };

		// Creating double array 2
		double[] doubleArray2 = new double[] { 1, 2, 3, 4, 5 };

		// Comparing arrays with Arrays.equals() method

		System.out.println("Double arrays are equal : " + Arrays.equals(doubleArray1, doubleArray2));

		// Creating boolean array 1
		boolean[] booleanArray1 = new boolean[] { true, true, false };

		// Creating boolean array 2
		boolean[] booleanArray2 = new boolean[] { true, true, false };

		System.out.println("boolean arrays are equal : " + Arrays.equals(booleanArray1, booleanArray2));
	}
}
Output:
Double arrays are equal : true
boolean arrays are equal : true

3. Comparing the Object Array using Arrays.equals()


Let us compare the Object arrays which holds the integer array.

Look the at below tricky example.
package com.javaprogramto.arrays.compare;

import java.util.Arrays;

public class ArraysCompareObjectIntegerArray {

	public static void main(String[] args) {

		// Creating integer array 1
		int[] intArray1 = new int[] { 1, 2, 3, 4, 5 };

		// Creating integer array 2
		int[] intArray2 = new int[] { 1, 2, 3, 4, 5 };

		// Creating two object arrays.

		Object[] objectArray1 = new Object[] { intArray1 };
		Object[] objectArray2 = new Object[] { intArray2 };

		// Comparing arrays with Arrays.equals() method
		boolean isSame = Arrays.equals(objectArray1, objectArray2);
		System.out.println("\nArray comparision with Arrays.equals(objectArray1, objectArray1)");
		if (isSame) {
			System.out.println("int objectArray1 and objectArray2 are same");
		} else {
			System.out.println("objectArray1 and objectArray2 are not same");
		}
	}
}

Output:
Array comparision with Arrays.equals(objectArray1, objectArray1)
objectArray1 and objectArray2 are not same

When we compared the objects arrays, it did not compare the values inside the integer arrays. If it was compared then it would have returned true because we have the same values in those int arrays.

4. Comparing Multi Dimension Arrays using Arrays.equals()


Further, compare the Multi Dimension Arrays using the same equals() method.
package com.javaprogramto.arrays.compare;

import java.util.Arrays;

public class ArraysCompareMultiDimArray {

	public static void main(String[] args) {

		// Creating integer array 1
		int[] intArray1 = new int[] { 1, 2, 3 };

		// Creating integer array 2
		int[] intArray2 = new int[] { 1, 2, 3 };

		// Creating integer array 3
		int[] intArray3 = new int[] { 1, 2, 3 };

		// Creating integer array 4
		int[] intArray4 = new int[] { 1, 2, 3 };

		// Creating two object arrays.

		int[][] intMultiArray1 = new int[][] { intArray1, intArray3 };
		int[][] intMultiArray2 = new int[][] { intArray2, intArray4 };

		// Comparing arrays with Arrays.equals() method
		boolean isSame = Arrays.equals(intMultiArray1, intMultiArray2);
		System.out.println("\nArray comparison with Arrays.equals(intMultiArray1, intMultiArray2)");
if (isSame) { System.out.println("int intMultiArray1 and intMultiArray1 are same"); } else { System.out.println("intMultiArray1 and intMultiArray1 are not same"); } } }

Output:
Array comparison with Arrays.equals(intMultiArray1, intMultiArray2)
intMultiArray1 and intMultiArray1 are not same
Observe the output as it returned false because equals() method does not compare inner or nested arrays.

5. Comparing Multi Dimension Arrays using Arrays.deepEquals()


To address the object array with another type array and nested arrays problems, Arrays class has another method to compare the contents deeply with deepEquals() method.

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

Now, let us try the examples from sections 3 and 4 using deepEquals() methods.
package com.javaprogramto.arrays.compare;

import java.util.Arrays;

public class ArraysCompareDeepEquals {

	public static void main(String[] args) {

		// Example 1 : Comparing object arrays with deepEquals() method

		// Creating integer array 1
		int[] intArray1 = new int[] { 1, 2, 3, 4, 5 };

		// Creating integer array 2
		int[] intArray2 = new int[] { 1, 2, 3, 4, 5 };

		// Creating two object arrays.

		Object[] objectArray1 = new Object[] { intArray1 };
		Object[] objectArray2 = new Object[] { intArray2 };

		System.out.println("Object arrays comparision with deepEquals() method");
		System.out.println("objectArray1 and objectArray2 are  : "
				+ (Arrays.deepEquals(objectArray1, objectArray2) ? "same" : "not same"));

		// Example 2 : Comparing integer nested arrays with deepEquals() method

		// Creating integer array 1
		intArray1 = new int[] { 1, 2, 3 };

		// Creating integer array 2
		intArray2 = new int[] { 1, 2, 3 };

		// Creating integer array 3
		int[] intArray3 = new int[] { 1, 2, 3 };

		// Creating integer array 4
		int[] intArray4 = new int[] { 1, 2, 3 };

		// Creating two object arrays.

		int[][] intMultiArray1 = new int[][] { intArray1, intArray3 };
		int[][] intMultiArray2 = new int[][] { intArray2, intArray4 };

		// Comparing arrays with Arrays.deepEquals() method
		System.out.println("intMultiArray1 and intMultiArray1 are  : "
				+ (Arrays.deepEquals(intMultiArray1, intMultiArray2) ? "same" : "not same"));

	}
}

Output:
Object arrays comparision with deepEquals() method
objectArray1 and objectArray2 are  : same
intMultiArray1 and intMultiArray1 are  : same
Returns true for both cases. So, deepEquals() method works fine with nested and object arrays which hold the other types of arrays.

6. Conclusion


In this article, You've seen how to compare two arrays in java using Arrays.equals() and Arrays.deepEquals() methods.

Arrays.equals() method works only the normal array but whereas Arrays.deepEquals() method works for nested arrays and object type arrays. And also deepEquals() works for nested object arrays which refer to the different type arrays.

Also, the same two methods work in java 8.



Read Next


Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.