$show=/label

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

SHARE:

A quick guide and understand how to compare two or more arrays in java and also comparing arrays using 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 deep level technique.

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

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

2. Comparing Arrays Using Arrays.equals()


Arrays.equals() method does comparing the contents of two given arrays. This method returns true if the all values are same in two arrays and returns false if contents are not 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 the 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, Created two arrays with same values. And next, two arrays are compared with == operator and it returned false because it does comparing the references rather than values (even though values are 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 arrays to equals() method but two arrays should be 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 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 frkm section 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 holds the another type 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 where as Arrays.deepEquals() method works for nested arrays and object type arrays. And alos deepEquals() works for nested object arrays which references to the different type arrays.

Also the same two methods work in java 8.

GitHub


Read Next


Ref

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,16,Arrays,16,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,5,Collections,23,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,84,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,12,Dictionary,1,Difference,1,Download,1,Eclipse,2,Efficiently,1,Error,1,Errors,1,Exception,1,Exceptions,3,Fast,1,Files,10,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,4,Grant,1,Grep,1,HashMap,1,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,5,Iterate,2,Jackson API,3,Java,30,Java 10,1,Java 11,5,Java 12,5,Java 13,2,Java 14,2,Java 8,77,Java 8 Difference,2,Java 8 Stream Conversions,2,java 8 Stream Examples,3,Java 9,1,Java Conversions,11,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,93,java.lang,5,java.util. function,1,jQuery,1,Kotlin,10,Kotlin Conversions,3,Kotlin Programs,6,Lambda,1,lang,29,Leap Year,1,live updates,1,Logging,1,Mac OS,2,Math,1,Matrix,5,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,23,String,58,String Programs,12,String Revese,1,Swing,1,System,1,Tags,1,Threads,10,Tomcat,1,Tomcat 8,1,Troubleshoot,16,Unix,2,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How To Compare Two Arrays In Java and New Java 8 API
How To Compare Two Arrays In Java and New Java 8 API
A quick guide and understand how to compare two or more arrays in java and also comparing arrays using java 8 api.
JavaProgramTo.com
https://www.javaprogramto.com/2020/11/compare-two-arrays-in-java.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/11/compare-two-arrays-in-java.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content