This post shows how to compare two multidimensional arrays of reference types in Java. The codes will examine if they are the same in terms of element values and their locations. For example, we want to compare a modified array with the original copy of that array.
Java Compare Basics
To make Java compare objects in a non-default way, we need to override some class methods. These methods are hashCode and equals. Overriding them gives us the chance to decide how we compare objects using their properties – instance variables. We may not want to include all object properties in a comparison. For instance, we only need to compare two Persons’ last names and not their first names or ages.
Beyond object-to-object comparison, we can compare multidimensional arrays of objects. The same principle applies, but we are comparing a group of objects with another group.
Using Arrays.deepEquals With Multidimensional Arrays of Strings
Here we have two multidimensional arrays, and we have to know if they are the same or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.turreta.array; import java.util.Arrays; public class MultiDimensionalArrayCompareEqualDemo { public static void main(String[] args) { String[][] strOriginalValues = { { "a", "b", "c" }, { "d", "e", "f", "g" }, { "h", "i", "j" } }; String[][] strCopyValues = { { "1", "b", "c" }, { "d", "e", "f", "g" }, { "h", "i", "j", "k" } }; System.out.println(Arrays.deepEquals(strCopyValues, strOriginalValues)); } } |
The codes output false because the two arrays are different. The strCopyValues has a value of “1” at strCopyValues[0][0] and an additional element “k.”
1 | false |
Using Arrays.deepEquals With Multidimensional Arrays of
Other Reference Types
Here is an example that uses the Person objects instead of String objects for multidimensional arrays in Java. We will compare the multidimensional arrays similarly, but we need to override the hashCode and equals methods to facilitate the Java comparison.
Person Class
We have the following codes, and we overrode the hashCode and equals methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | class Person { private String id; private String name; private Pet pet; public Person(String id, String name, Pet pet) { super(); this.id = id; this.name = name; this.pet = pet; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } /** * Generated using Eclipse's "Generate constructor using fields..." feature */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((pet == null) ? 0 : pet.hashCode()); return result; } /** * Generated using Eclipse's "Generate constructor using fields..." feature */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (pet == null) { if (other.pet != null) return false; } else if (!pet.equals(other.pet)) return false; return true; } } |
Pet Class
To make the codes more interesting, each Person object can have a Pet object. We also overrode its hashCode and equals methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Pet { private String petName; public Pet(String petName) { super(); this.petName = petName; } public String getPetName() { return petName; } public void setPetName(String petName) { this.petName = petName; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((petName == null) ? 0 : petName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Pet other = (Pet) obj; if (petName == null) { if (other.petName != null) return false; } else if (!petName.equals(other.petName)) return false; return true; } } |
Main class
Now let us look at our main class that uses the Person and Pet classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.turreta.array; import java.util.Arrays; public class MultiDimensionalArrayCompareEqualDemo2 { public static void main(String[] args) { Person[] originalPersons = { new Person("1", "Steve", new Pet("pet a")), new Person("2", "Andrew", new Pet("pet b")) }; Person[] copyOfPersons = { new Person("1", "Steve", new Pet("pet a")), new Person("2", "Andrew", new Pet("pet b")) }; System.out.println(Arrays.deepEquals(originalPersons, copyOfPersons)); } } |
Output
1 | true |
This is (now) part of a reboot Java tutorial.