OOPS.................

Thursday, March 5, 2009

How To Decide the valid hashCode() when ovveride the equals in the class

public class ValuePair implements Comparable{
private int iLookUp;
public ValuePair(int iLookUP, String sValue){
this.iLookUp=iLookUp;
}


public void setLookUp(int iLookUp){
this.iLookUp = iLookUp;
}
public int getLookUp(){
return iLookUp;
}

public boolean equals(Object o){
Integer iwLookUp = (Integer) o;
if(iLookUp == iwLookUp.intValue()){
return true;
}
return false;
}


public int compareTo(Object o) {
ValuePair vp = (ValuePair) o;
Integer iwLookUp= new Integer(vp.getLookUp());
if(iwLookUp.intValue() < iLookUp){
return -1;
}

if(iwLookUp.intValue() > iLookUp){
return +1;
}
return 0;
}

}

The valid hashCode method are





public int hashCode(){
return iLookUp;
}



public int hashCode(){
return iLookUp * 100;
}