Ad

Sunday 30 August 2009

overriding hashcode and equals method

We need to override the hashcode and equals method for the object that has to be added in Collections which uses hash technique.Hence for collection like hashset,hashmap,linkedhashset and hashtable we need to override both the equals and hashcode method .

what does it really means overriding hashcode and equals method?why we need to do it?
    Map stores the object as Key-value pair.we will retrieve the value from Map,by passing key to it.when we add an key/value pair to the map,the hashcode of the Key(object) will be used to allocate a slot in the collection.Its something like an seat no where an object in the collection sits.To retrieve the value from Map,the same hashcode will be used to locate the object in Map.

Lets take a below sample class

Class Department
{
String name;
public Department(String name)
{
this.name=name;
}
public static void main (String [] args) {
HashMap map = new HashMap(); //Initialize the HashMap
Department d1= new Department("CHEMICAL"); //construct a Department Object
Department d2= new Department("PHYSICS"); //construct a Department Object
map.put(d1,"CHEMICALLAB"); //Add the String "CHEMICALLAB" to Map with key as d1
map.put(d2,"PHYSICSLAB"); //Add the String "PHYSICSLAB" to Map with key as d2
retrieveMap(map);
}
public void retrieveMap(HashMap map) //To retrive the value from Map
{
Department dep1= new Department("CHEMICAL");
Department dep2= new Department("PHYSICS");
System.out.prinntln("LABNAME1:"+map.get(dep1));//Retrieve the value by passing the Key
System.out.prinntln("LABNAME2:"+map.get(dep2));//Retrieve the value by passing the Key

}
}

when we run the above program,we will get the below output

LABNAME1:null
LABNAME2:null


But the expected output was
LABNAME1:CHEMICALLAB
LABNAME2:PHYSICSLAB


when we add the object d1 as key and Value as String "CHEMICALLAB" to HashMap,object(Superclass of all Classes)hashcode will be used to allocate a seat to key d1 in hashmap.The object hashcode method always return distinct integers for distinct objects.when we pass the key to retrieve the value associated ,it will locate the Key in the collection whose
(1) hashcode is same as the object hashcode which we passed
(2) key object in collection is same as passed key object according to the equals method.
Once the Key is found,the value associated with the key will be returned.

But we could not able to retrieve the value from haspmap,since we didn't override both the method.while we try to retrieve the value from hashmap in the above example
(1)object hashcode will be executed.Since the object d1(Key in collection) and dep1(key we passed) are distict objects,the hashcode method will return distinct values
(2) object equals method will be executed and it will return false. The object equals method will return true only if the the two references refer the same object ie if d1==dep1

Add the below methods to Department Class to override the hashcode and equals method

public boolean equals(Object obj) {                                                                                                          
Department key = (Department)obj;
return name.equals(key.name()) ;
}

public int hashCode() {
return name.hashCode();
}


and now the output will be as expected

LABNAME1:CHEMICALLAB
LABNAME2:PHYSICSLAB

No comments:

Post a Comment