2

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.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Gundamaiah
  • 780
  • 2
  • 6
  • 30

5 Answers5

1

Logic is that a Map gives you out the same value for the same key. Your e3 is exactly the same as e1 (from the map's persepective, since you've overridden equals() and hashcode()), and you did add a String with e1 as the key into the map.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • But I didnt add the e3 to the Map.Then how it will come from the map. – Gundamaiah Feb 09 '14 at 17:01
  • You add a String to the map with e1 as the key. Now any object that is equal to e1 (like e3) will retrieve the String, that's how Maps work. – Kayaman Feb 09 '14 at 17:09
0

It depends on how equals and hashcode method has been implemented in your Employee class.If it has been so implemented to consider objects having same values as equal objects,of course it would fetch

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
0

e3 has the same field values as e1. Thus, as far as the map is concerned, it's equivalent to e1. You're looking it up by the same key and are therefore returning the value you added for e1, "Software Developer."

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
0

map.get(Object) will look for an object in the HashMap that exists in there based on the rules defined in .equals() and hashCode(). Therefore it will return the object that is identical to the searched one (the first object in your case).

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

That basically depends on the implementation of the Employee class methods equals and hashCode.

Your implementation certainly does return the same value for e3 than from either e1 and e2.

The Map stores Employees and maps them to String. If you ask for e3 the map will look, if it already contains e3 and does this according to the rules you've implemented with equals and hashmap. If it finds an entry, which has the same value there, it will return it - thus I suspect that either e1 or e2 have the same values as e3 there.

If the implementation is based on 'eid' and 'ename' than e1.equals(e3) and e1.hashCode() == e3.hashCode() and thus they are interchangeable.

A word of warning: Using mutable classes as keys in maps can lead to problems if you mutate (change) the values afterwards. To try this out, just do the following:

 map.put(e1);
 e1.setEid(3);
 e1.setEname("foo");
 System.out.println(map.contains(e1));   // Will print 'false'

I would either recommend not to do this, make the class immutable (that is, make the fields final) or be absolutely sure that no one is gonna change them (which will happen sooner or later anyway...)

tilois
  • 682
  • 5
  • 15