Compare two objects in Java
This post will discuss how to compare two objects in Java.
You should never use the == operator for comparing two objects, since the == operator performs a reference comparison and it simply checks if the two objects refer to the same instance or not. The recommended option to compare two objects is using the equals() method. However, simply calling the equals() method won’t work as expected, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class TV { String company; String model; int warranty; public TV(String company, String model, int warranty) { this.company = company; this.model = model; this.warranty = warranty; } } public class Main { public static void main(String[] args) { TV tv1 = new TV("Vu", "Vu Premium 4K TV", 2); TV tv2 = new TV("Vu", "Vu Premium 4K TV", 2); System.out.println(tv1.equals(tv2)); // false } } |
The above program will call the equals() method of the Object class, which behaves similar to the == operator as it is not overridden by a class. Therefore, every class should override the equals (and hashCode) method of the Object class and specify the equivalence relation on objects, such that it evaluates the comparison of values in the object irrespective of whether two objects refer to the same instance or not. See this post for more details.
Here’s a simple program that demonstrates the working of the equals() method.
|
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 |
import java.util.Objects; class TV { String company; String model; int warranty; public TV(String company, String model, int warranty) { this.company = company; this.model = model; this.warranty = warranty; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TV tv = (TV) o; return warranty == tv.warranty && Objects.equals(company, tv.company) && Objects.equals(model, tv.model); } @Override public int hashCode() { return Objects.hash(company, model, warranty); } } public class Main { public static void main(String[] args) { TV tv1 = new TV("Vu", "Vu Premium 4K TV", 2); TV tv2 = new TV("Vu", "Vu Premium 4K TV", 2); System.out.println(tv1.equals(tv2)); // true } } |
You must also override the hashCode() method, since equal objects must have equal hash codes. If you prefer not to override the equals() and hashCode() method, you can create a custom function to check for equality. This is demonstrated below:
|
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 |
import java.util.Objects; class TV { String company; String model; int warranty; public TV(String company, String model, int warranty) { this.company = company; this.model = model; this.warranty = warranty; } } public class Main { public static boolean isEqual(TV tv1, TV tv2) { if (tv1 == tv2) { return true; } if (tv2 == null || tv1.getClass() != tv2.getClass()) { return false; } return tv1.warranty == tv2.warranty && Objects.equals(tv1.company, tv2.company) && Objects.equals(tv1.model, tv2.model); } public static void main(String[] args) { TV tv1 = new TV("Vu", "Vu Premium 4K TV", 2); TV tv2 = new TV("Vu", "Vu Premium 4K TV", 2); System.out.println(isEqual(tv1, tv2)); // true } } |
That’s all about comparing two objects in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)