equals() and hashCode() methods are required when you insert objects in s collection. Both play a very important role for Java objects. Both methods are defined in Object class. As Object class is the parent of all classes by default, so we override these methods in our objects and give our own definitions.
equals()
equals() is required to compare the properties of one object with other. equals() method uses identity operator(= =)to determine whether object are equal. In equals()we compare only those properties on the basis of them objects are compared. For primitive data types like int, double identity operator is sufficient but in case on objects we need more functionality for comparing. For example:
class Employee{
private String name;
private int age;
public Employee(String name, int age){
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj){
if(obj != null && obj instanceof Employee){
if(((Employee) obj).name.equals(this.name) && ((Employee) obj).age == this.age){
return true;
} else
return false;
} else
return false;
}
}
Comparing Employee class objects.
Employee e1 = new Employee("john", 25);
Employee e2 = new Employee("Dev", 26);
System.out.println(e1.equals(e2)); // false
hashcode()
hashCode() method is used when object are inserted in HashMap, HashTable, HashSet. When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object.
The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The hashtable then iterates this area (all keys with the same hash code) and uses the key's equals() method to find the right key. Once the right key is found, the object stored for that key is returned.
Here are two rules that are good to know about implementing the hashCode() method in your own classes, if the hashtables in the Java Collections API are to work correctly:
- If object1 and object2 are equal according to their equals() method, they must also have the same hash code.
- If object1 and object2 have the same hash code, they do NOT have to be equal too.
In shorter words:
- If equal, then same hash codes too.
- Same hash codes no guarantee of being equal.
class Employee{
private String name;
private int age;
public Employee(String name, int age){
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj){
if(obj != null && obj instanceof Employee){
if(((Employee) obj).name.equals(this.name) && ((Employee) obj).age == this.age){
return true;
} else
return false;
} else
return false;
}
@Override
public int hashCode(){
return this.name.hashCode() * this.age; // calculating hashCode using Employee properties
}
}
Please post comments !
No comments:
Post a Comment