-4

I have written the below program and not able to understand the output.

package com.demo.strings;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

class Emp
{
    private int empNo;
    private String empName;
    public Emp(int empno, String empname)
    {
        this.empNo=empno;
        this.empName=empname;
    }
    public int getEmpNo() {
        return empNo;
    }
    public void setEmpNo(int empNo) {
        this.empNo = empNo;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public boolean equals(Object obj)
    {
        if(obj != null && obj instanceof Emp)
        {
            String name = ((Emp)obj).getEmpName();
            if(name.equals(this.getEmpName()))
            {
                return true;
            }
        }
        return false;
    }
    public int hashcode()
    {
        return (this.hashCode()+1);
    }
}

public class StringDemo {

    public static void main(String[] args) {
        Emp e1 = new Emp(1,"Stack");
        Emp e2 = new Emp(1,"Stack");

        Map<Integer,Emp> empMap = new HashMap<Integer,Emp>();

        empMap.put(1,e1);
        empMap.put(2,e2);


        System.out.println(empMap.size());
        System.out.println("Both objects are equal: "+e1.equals(e2));
    }
}

output:2 Both objects are equal: true

I've overridden equals and hashcode, so Map is supposed to store only one object in it. But it appears to be storing both of them. Is the way equals and hashcode were overridden wrong? Can anyone explain me how to fix this?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Pardhu
  • 1
  • 2
  • 2
    Possible duplicate of [Why do I need to override the equals and hashCode methods in Java?](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) – Sangam Belose Dec 14 '17 at 05:25
  • 1
    Are you sure that `hashcode()` doesn't throw a `StackOverflowError`? It will recurse infinitely. – MC Emperor Dec 14 '17 at 12:02

1 Answers1

1

First read basics from here

You are using HashMap. It Stores only one unique key.

    Map<Integer,Emp> empMap = new HashMap<Integer,Emp>();

    empMap.put(1,e1);
    empMap.put(2,e2);

In above three lines you are trying to store two elements with different keys (1 and 2). So you are getting output as "2 Both objects are equal: true"

//Try this
Map<Integer,Emp> empMap = new HashMap<Integer,Emp>();

empMap.put(1,e1);
empMap.put(1,e2);

In this case second put will replace first value e1 with e2. But as you override equals to compare empName field it gives output as "1 Both objects are equal: true"

If you change the declaration of e1 and e2 as

Emp e1 = new Emp(1,"Stack One");
Emp e2 = new Emp(1,"Stack Two");

Output will be

1
Both objects are equal: false
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abhishek
  • 3,348
  • 3
  • 15
  • 34
  • Its my bad. I thought i have added empMap.put(1,e1); empMap.put(1,e2); thanks a lot Abhishek – Pardhu Dec 14 '17 at 08:03