The equals() and hashCode() methods are very important for all objects in Java. In this tutorial, I will present to you all the basic knowledge about these two methods.
By default, these methods are defined in Object objects, which are the parent objects of all objects in Java. But to use the right purpose of these two methods, we need to override them in our objects.
Equals() method
This method is used to test whether two objects are equal or not?
As you know, in Java we have two basic data types, primitive and object. For primitive variables, we can use the “==” operator to compare any two variables, for example:
1 2 3 4 5 6 7 8 9 10 11 |
package com.huongdanjava.example; public class Example { public static void main(String[] args) { int b = 2; int c = 2; System.out.println(b == c); } } |
Result:
But for the object data type, we must override the equals() method in the Object object to compare whether two objects of the same class are equal.
For example, I have a Student object like this:
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 |
package com.huongdanjava.example; public class Student { private String firstName; private String middleName; private String lastName; private String country; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } } |
Now, I will use the equals() method that the Student object inherits from Object to compare whether the two Student objects have the same firstName, middleName, lastName, and country are equal or not?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.example; public class Example { public static void main(String[] args) { Student student1 = new Student(); student1.setFirstName("Khanh"); student1.setMiddleName("Huu"); student1.setLastName("Nguyen"); student1.setCountry("Viet Nam"); Student student2 = new Student(); student2.setFirstName("Khanh"); student2.setMiddleName("Huu"); student2.setLastName("Nguyen"); student2.setCountry("Viet Nam"); System.out.println(student1.equals(student2)); } } |
Result:
As you can see, although 2 Students have the same name and country, Java does not see them as one. To solve this problem, we will override the equals() method in the Student object as follows:
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 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.huongdanjava.example; public class Student { private String firstName; private String middleName; private String lastName; private String country; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getMiddleName() { return middleName; } public void setMiddleName(String middleName) { this.middleName = middleName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (country == null) { if (other.country != null) return false; } else if (!country.equals(other.country)) return false; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; if (middleName == null) { if (other.middleName != null) return false; } else if (!middleName.equals(other.middleName)) return false; return true; } } |
In the above equals() method, I checked all the properties of the Student object. Only if all properties of two student1 and student2 objects are the same, this method returns true. And then, Java will see that these two objects are equal.
Then the result will be:
HashCode() method
Objects such as HashMap, HashTable use key objects to search for corresponding value objects. And for optimal search, Java uses hash values derived from the hashCode() method of key objects to group these objects in so-called buckets. Each bucket corresponds to a hash value.
When searching for any object in the HashMap or HashTable, Java first gets the value of the key object we want to search for. It then checks the buckets to see if the value of the hash value matches the bucket. If a bucket is satisfied, Java will return the value of the value object that we want to search.
Multiple objects can have the same hash value, and therefore they can share the same bucket. In this case, Java will use the equals() method to find exactly the value object we want to find.
Here we have a concept called contract between the equals() and hashCode() methods:
- If two objects are equal then they must have the same hashCode().
- If two objects have the same hashCode() then they can be equal or not.