Besides sorting, searching, copying and filling, the java.util.Arrays class also provides convenient methods for comparing two arrays and returning hash code an array. In this article, we’re going to help you understand these functionalities in details with full code examples.

 

1. Comparing two arrays

The Arrays utility provides the following methods for comparing two arrays of same type:

boolean equals(X[] a, X[] a2)

boolean equals(Object[] a, Object[] a2)

Here, X denotes a primitive type (byte, short, char, int, float, and double). Two arrays are considered equal if they contain the same elements in the same order. Let’s see some code examples.

The following code compares two arrays of integer numbers:

int[] numbers1 = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
int[] numbers2 = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
int[] numbers3 = {1, 2, 5, 7, 9, 11, 13, 15, 17, 19, 21};

boolean equal = Arrays.equals(numbers1, numbers2);

System.out.println("numbers1 == numbers2? " + equal);

equal = Arrays.equals(numbers1, numbers3);

System.out.println("numbers1 == numbers3? " + equal);
Output:

numbers1 == numbers2? true
numbers1 == numbers3? false
 

The following example compares two arrays of Strings:

String[] headings1 = {"No", "Name", "Email", "Address", "Country"};
String[] headings2 = {"No", "Name", "Email", "Address", "Country"};
String[] headings3 = {"No", "Name", "Email", "Country", "Address"};

boolean equal = Arrays.equals(headings1, headings2);

System.out.println("headings1 == headings2? " + equal);

equal = Arrays.equals(headings1, headings3);

System.out.println("headings1 == headings3? " + equal);
Output:

headings1 == headings2? true
headings1 == headings3? false


 

When comparing two arrays of a custom reference type, the class must override the equals()method properly. Given the following entity class:

class Person {
	String name;
	int age;

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public boolean equals(Object obj) {
		if (obj instanceof Person) {
			Person another = (Person) obj;
			if (this.name.equals(another.name)
				&& this.age == another.age) {
					return true;
			}
		}

		return false;
	}
}
The following code compares two arrays of Person objects:

Person tom = new Person("Tom", 55);
Person adam = new Person("Adam", 26);
Person jane = new Person("Jane", 31);
Person john= new Person("John", 40);
Person dave = new Person("Dave", 22);

Person[] persons1 = {tom, adam, jane, john, dave};
Person[] persons2 = {tom, adam, jane, john, dave};
Person[] persons3 = {tom, john, jane, adam, dave};

boolean equal = Arrays.equals(persons1, persons2);

System.out.println("persons1 == persons2? " + equal);

equal = Arrays.equals(persons1, persons3);

System.out.println("persons1 == persons3? " + equal);
Output:

persons1 == persons2? true
persons1 == persons3? false
 

The equals() methods work correctly with single dimension array. For comparing multi-dimensional arrays, consider using the deepEquals() method:

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

This method returns true if the two specified arrays are deeply equal to one another. This method is appropriate for use with nested arrays of arbitrary depth.

Here’s an example that compares two 2-dimenson arrays:

String[][] data1 = {
	{"Name", "City", "Country"},
	{"Bill", "Hawaii", "USA"},
	{"David", "Madrid", "Spain"},
};

String[][] data2 = {
	{"Name", "City", "Country"},
	{"Bill", "Hawaii", "USA"},
	{"David", "Madrid", "Spain"},
};

String[][] data3 = {
	{"Name", "City", "Country"},
	{"David", "Madrid", "Spain"},
	{"Bill", "Hawaii", "USA"},
};

boolean equal = Arrays.deepEquals(data1, data2);

System.out.println("data1 == data2 ? " + equal);

equal = Arrays.deepEquals(data1, data3);

System.out.println("data1 == data3 ? " + equal);
Output:

data1 == data2 ? true
data1 == data3 ? false
 

Note that, two array references are considered equal if both are null. For example:

byte[] bytes1 = null;
byte[] bytes2 = null;

boolean equal = Arrays.equals(bytes1, bytes2);

System.out.println("bytes1 == bytes2 (both null) ? " + equal);

String[][] data1 = null;
String[][] data2 = null;

System.out.println(Arrays.deepEquals(data1, data2));
Output:

bytes1 == bytes2 (both null) ? true
true
 

2. Returning hash code of an array

The Arrays class also provides convenient methods that return hash code of a specified array (X denotes a primitive type):

  • int hashCode(X[] a)
  • int hashCode(Object[] a)
This method can be useful in case we want to include hash code of an array to calculate hash code of a class of which the array is a member.

The following class explains a scenario that uses Arrays.hashCode() method:

class Student {
	String name;
	int[] marks;

	public Student(String names, int[] marks) {
		this.name = name;
		this.marks = marks;
	}

	public int hashCode() {
		return name.hashCode() + Arrays.hashCode(marks);
	}
}
Similarly, the deepHashCode(Object[] a) method is designed to return hash code based on the deep content of a multi-dimensional array.

 

References:

 

Related Java Arrays Tutorials:

 

Other Java Collections Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (ArraysUtilExamples3.java)ArraysUtilExamples3.java[Java Arrays Utilitly Examples]3 kB

Add comment