-2

Can some one tell me whats wrong with my code. I am not able to figure it out even though following the link Why do I need to override the equals and hashCode methods in Java?

import com.sun.org.apache.xpath.internal.operations.Equals;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) {

        Employee a = new Employee(1, "Ram");
        Employee b = new Employee(1, "Ram");
        Employee c = a;
        System.out.println(b.equals(a));
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());

        HashMap<Employee, String> hm = new HashMap<Employee,String>();
        hm.put(a, "one");
        System.out.println(hm.containsKey(b));

        Iterator<Map.Entry<Employee, String>> itr = hm.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry<Employee,String> ent = itr.next();
            System.out.println("key "+ent.getKey() +" value " +ent.getValue());
      }


    }

}

class Employee {

    private int id;
    private String name;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public boolean equals(Employee obj)
    {
           if(this.id==obj.id)
               return true;
           else
               return false;
    }
    public int hashCode()
    {
        int result =1;
        return (result*37+(name!=null? name.hashCode():0));
    }



}

I am overriding equals and hashcode method such that my two employee objects are same. When i search for b object in map,since b's hashCode is same as that of a, next it searches for equality on a and b which is true. Buy why hm.contains(b) returns false in my code, which should be true

Community
  • 1
  • 1
user3607869
  • 317
  • 1
  • 9
  • 19
  • 1
    We need to see your Employee `equals` and `hashcode` implementations. – Kedar Mhaswade May 09 '16 at 16:54
  • Can you please share the code for `Employee`? The problem is probably there, and not in the code you've shared. – Mureinik May 09 '16 at 16:54
  • the signature of the `equals` method is always `public boolean equals(Object obj)` - try adding `@Override` to yours. – zapl May 09 '16 at 16:59
  • 2
    `equals(Employee obj)` is **not** an override/implementation of `equals(Object obj)`. You must implement `equals(Object obj)` and check `obj instanceof Employee` in the code. Add `@Override` to both `equals()` and `hashCode()` methods to ensure that you do not fall into this kind of error again. – Andreas May 09 '16 at 16:59
  • It helped...thanks a lot – user3607869 May 09 '16 at 17:01
  • Side note: consider learning about unit tests instead of using static mains for such kind of testing. – GhostCat May 09 '16 at 17:09

1 Answers1

1

Your equals method has to override Object.equals(Object obj):

public boolean equals(Employee obj)

should be

public boolean equals(Object obj)
Skip Head
  • 7,580
  • 1
  • 30
  • 34