I wrote an Employee
class with eid
and ename
with setters and getters and I override equals()
and hashcode()
.
Now I wrote another class HashMapTest
in which I created three Employee
objects. I added two of them to the HashMap
and the third one is same as the first employee object.
My HashMapTest class is below:
public class HashMapTest {
public static void main(String[] args) {
Employee e1 = new Employee();
e1.setEid(1);
e1.setEname("Ganesh");
Employee e2 = new Employee();
e2.setEid(1);
e2.setEname("Mahesh");
Map<Employee, String> map = new HashMap<Employee, String>();
map.put(e1, "Software Developer");
map.put(e2, "Software Test Engineer");
Employee e3 = new Employee();
e3.setEid(1);
e3.setEname("Ganesh");
System.out.println("Getting employee e3 details " + map.get(e3));
}
}
I am not adding e3
to the HashMap
, but I'm still getting it from the HashMap
.
Please explain the logic behind this.