In
Java:
By definition, if two objects are equal, their hash code must also be equal. If you override the equals() method, you change the way two objects are equated and Object's implementation of hashCode() is no longer valid.Therefore, if you override the equals() method, you must also override the hashCode() method as well.
I want to understand, how to override the hascode(). Say for example I have a class where I use:
String s3 = new String ("hello");
String s4 = new String ("hello");
if(s3.equals(s4) ){ // I am overriding the equals() method.
//do something....
}
HOW TO OVERRIDE HERE THE hashCode()?(need code example for this scenario.)
OR,
say another example of code I have is:
Test t = new Test();
Test p = new Test();
if(t.equals(p)){
//do something....
}
HOW TO OVERRIDE HERE THE hashCode()?(need code example for this scenario.)
I would appreiciate if someone can show the code example to override the hashcode() for above both examples.
thanks in advace friends...
[ June 09, 2008: Message edited by: Ramesh Kumar Swarnkar ]
[ June 09, 2008: Message edited by: Ramesh Kumar Swarnkar ]