Possible Duplicate:
Overriding equals and hashCode in Java
package testpack;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class TestEm {
    private String company;
    private int salary;
    TestEm(String company, int salary) {
        this.company = company;
        this.salary = salary;
    }
    public static void main(String[] args) {
        Map<TestEm, String> map = new HashMap<TestEm, String>();
        map.put(new TestEm("abc", 100), "emp1");
        map.put(new TestEm("def", 200), "emp2");
        map.put(new TestEm("ghi", 300), "emp3");
        Set<TestEm> set = map.keySet();
        Iterator<TestEm> it = set.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        **System.out.println(map.get(new TestEm("ghi", 300)));**
    }
    @Override
    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof TestEm))
            return false;
        TestEm te = (TestEm) o;
        return te.company.equals(company) && te.salary == salary;
    }
    @Override
    public int hashCode() {
        int code = 0;
        code = salary * 10;
        return code;
    }
    /*
     * @Override public String toString() {
     * 
     *    return this.company;  }
     */
}
and the output is
testpack.TestEm@3e8 testpack.TestEm@7d0 testpack.TestEm@bb8 emp3
Hi, i have overridden some methods of Object class in my TestEm class, everything is okay but first three outputs are using iterator and then doing a S.O.P and the last one is simply with S.O.P, i just want to know that why first three outputs are coming as objects using iterator and the 4th one simply the correct output as value corresponding the key, i have not overridden toString() so first three outputs are right but then without toString() how S.O.P is showing the correct output.
 
     
     
     
     
    