-2

I have a custom class that has overridden hashcode() and equals() method

class Employee1 {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Employee1(int id) {
        super();
        this.id = id;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee1 other = (Employee1) obj;
        if (id != other.id)
            return false;

        return true;
    }
}

and in main class I am using a Map having Object as Key

Map<Integer,Employee1> g = new HashMap<>();
    Employee1 e = new Employee1(1);
    Employee1 e1 = new Employee1(2);
    g.put(1, e);
    g.put(2, e1);
    Employee1 e4 = g.get(1);
    e4.setId(3);
    for(Map.Entry<Integer,Employee1> e3:g.entrySet()) {
       System.out.println(e3.getKey()+" "+e3.getValue().getId());
    }

My question is how come the key of the map is changed even though I have overridden hashcode and equals methods,key should be same but I am able to get and set the id and its reflecting in the map

The output for above code is

1 3 2 2

Gus
  • 3,534
  • 1
  • 30
  • 39
Ram Somani
  • 231
  • 3
  • 12
  • 4
    What does the key of a map entry have to do with a property in the value object? And what does any of it have to do with `equals()` and `hashCode()` implementations in the value object? Answer: nothing. – Robby Cornelissen Jul 10 '19 at 10:02

2 Answers2

0

Methods equals() and hashCode() are used for keys in a HashMap not for values. To see the difference you can try to use your class Employee1 as a value in a Map or just to use HashSet<Employee1>, it uses HashMap under the hood.

Vladimir Pligin
  • 1,547
  • 11
  • 18
0

"how come the key of the map is changed" - it does not change, as you can see the 1 and 2 in the output which are what you originally used in the put.

What you can change are the contents of what the object inside the map points to, the value. The map does not copy the Employee1 when you put it into the map, it retains a reference to the same instance which you know as e. If you get that value from the map e4 now is the exact same thing as e.

Is Java “pass-by-reference” or “pass-by-value”? may be helpful to you.

What you should never do is to mutate something that is involved in the hashCode computation (if there is a chance somebody actually relies on the hashcode). Because after the change the hashcode of the object changes, the HashMap (or HashSet) does not see the change since it does not recompute the hashcode since there is no way for it to know you mutated something (relevant).

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • If I make object as key then also its changing its value Map g = new HashMap<>(); Employee1 e = new Employee1(1); Employee1 e1 = new Employee1(2); g.put(e,1); g.put(e1,2); e.setId(3); for(Map.Entry e3:g.entrySet()) { System.out.println(e3.getKey().getId()+"***************"+e3.getValue()); } again the output is 3 1 2 2 – Ram Somani Jul 10 '19 at 13:21
  • @RamSomani read the answer again and read through the link I posted. – luk2302 Jul 10 '19 at 13:26