Hi there .. according to the offical
java contract whenever u override equals u need to override hashcode as well. But in case u dont do it, the hashtable of the Object class .. the granddaddy of all the classes..would be inherited.. n it would be used..
the problem with ur code are as follows:
1. ur not using "tbl.put(new Equals(1),"1");" properly ur using a new object of Equlas as a key ....which needs hashcode (n u dont have it.. rt?)
2 "tbl.get(eq1).toString());" here ur trying to get the stored value using the older object created in "Equals eq1=new Equals(1);"..
So since both are different objects (though equal. but without hashcode).. u wont get ur stored value in the used HashTable.
Try this may be u get somthin here string is used as a key which has hashcode overiden
import java.util.Hashtable;
public class Equals {
private int id;
Equals(int id){
this.id=id;
}
public boolean equals(Object obj){
if((obj instanceof Equals)&&(this.id==((Equals)obj).id))return true;
else return false;
}
public static void main(String[] args) {
Hashtable tbl=new Hashtable();
Equals eq1=new Equals(1);
tbl.put("1",eq1);
System.out.println(tbl.get("1").toString());
}
}