-6

Couple of questions and explanation is required for hashcode and equals method in hashcode. Below are the questions :

  1. when does the hashcode and equals gets called during put or get ?
  2. When we are storing custom object, then i found in every tutorials that we need to override hashcode. But if I donot override either, then it produces the same result. Except when I use new operator . Below is sample program :

I am storing employee objects into the hashmap and then i didnot override hashcode and equals method, then the code works as expected.

 Employee e1= new Employee("ram", 1000);
        Employee e2= new Employee("sai", 1001);
        Employee e3= new Employee("krishna", 1002);


        System.out.println("e1.hashCode() : "+e1.hashCode());
        System.out.println("e2.hashCode() : "+e2.hashCode());
        System.out.println("e3.hashCode() : "+e3.hashCode());


        Map<Employee, String> map= new HashMap<Employee, String>();

        map.put(e1, "employee1");
        map.put(e2, "employee2");
        map.put(e3, "employee3");        


        for (Entry<Employee, String> entry : map.entrySet())
        {
            System.out.println(entry.getKey().getName() +"-"+entry.getKey().getid() + "-" + entry.getValue());
        }

        System.out.println(map.get(e3));        
        System.out.println(map.get(new Employee("krishna", 1002)));
  1. When you override equals, what things need to be taken care.
  2. When you override hashcode , what things need to be taken care.
  3. When you override only equals, then the how the default hashcode will behave.
  4. When you override only hashcode, then how the default equals will behave.*
kapex
  • 28,903
  • 6
  • 107
  • 121
  • 2
    Dude, it's like your not even trying. Those questions are answered in any result from first page of google for "java equals hashCode". – Filip Malczak Dec 27 '16 at 11:08
  • Try to add again same employee but as new instance: `Employee e4= new Employee("krishna", 1002);` then it will stop work as you expect. – RadekJ Dec 27 '16 at 11:10
  • As well as being a duplicate (of many others), your question is too broad, and you clearly have not adequately researched the question (e.g. googled for answers) before asking. – Stephen C Dec 27 '16 at 12:00

2 Answers2

0

Read this Article for a detail explanation.

In short even when you don't implement hashCode and equals in your classes there is a default implementation being inherited from Object class. That is why you don't see any issues.

But you will definitely have issues when using Collections.

shazin
  • 21,379
  • 3
  • 54
  • 71
-1

Hashcode and equals are needed to minimize collision. Your target will be to implement hashcode in such a way so that there is only one entry in a bucket. Equals will help you to distinguish between two entry where hashcodes are same. Hashcode and equals together decides if two keys are same. So if these are not taken care, two different custom object might be treated as same key and second one will replace the first one in map.

DS_
  • 71
  • 1
  • 6