1

i am adding objects to a hashSet , iam overriding hashcode() and equals() . i intentionally pass different instance variables in these

hashcode is diff but objects are equal still it is inserting both in hashset

code :

HashSet hs = new HashSet();
   employee e1 = new employee();
   employee e2 = new employee();
   e1.setname("amol1");  e1.setcity("bombay");
   e2.setname("amol1");  e2.setcity("delhi");
    System.out.println("e1 --> "+e1.hashCode()+ " and e2 --> " +e2.hashCode() + "  " + e1.equals(e2));
    hs.add(e1);  hs.add(e2)

overriden methods :

  @Override
public int hashCode()
{
   return city.hashCode();
}

 @Override
 public boolean equals(Object o)
 {
   employee e = (employee)o;
   return getname().equalsIgnoreCase(e.getname());
 }
Amol
  • 183
  • 2
  • 12
  • 3
    They may have the same `name` (and thus be "equal"), but they have different `hashCode`s! So they go to different buckets and equality isn't checked. – Elliott Frisch Feb 12 '16 at 03:37
  • isnt equality the priority for adding object in hashSet ... i read it in headfirst – Amol Feb 12 '16 at 03:43
  • @BenjaminLowry he wants to know why the objects are getting added even though they have the same value. – Jitin Kodian Feb 12 '16 at 03:43
  • @JitinKodian yeah I see his problem now, my bad – Benjamin Lowry Feb 12 '16 at 03:44
  • That's a broken implementation. There is a contract to follow for `equals` and `hashSet`. http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java?rq=1 – Thilo Feb 12 '16 at 03:47
  • 1
    If they are equal, they have to have the same hashCode. – Peter Lawrey Feb 12 '16 at 03:55

2 Answers2

0

If a employee with name "Andrucz" should be equals to a employee with name "andrucz", both instances should have same hashCode, so you have to generate it using toLowerCase() or toUpperCase().

Also remember to name a class respecting Java convention. Instead of employee, class should be named Employee.

Also remember to check whether equals method parameter is a instance of Employee class.

@Override
public int hashCode() {
   return getName().toUpperCase().hashCode();
}

 @Override
 public boolean equals(Object o) {
   if (o instanceof Employee) {
       Employee e = (Employee) o;
       return getName().equalsIgnoreCase(e.getName());
   }
   return false;
 }
andrucz
  • 1,871
  • 2
  • 16
  • 28
-1

Java is always pass by value. Java is pass by value (read the 3rd answer in this link)

HashSet<Employee> hs = new HashSet<Employee>();

Employee employee1 = new Employee();
Employee employee2 = new Employee();

employee1.setCity("delhi");
employee2.setCity("delhi");

employee1.setName("amol");
employee2.setName("amol");

hs.add(employee1);
hs.add(employee2);

System.out.println(hs.size());

The output will be 2.

hs = new HashSet<Employee>();
employee1 = new Employee();

employee1.setCity("delhi");
employee1.setName("amol");

employee2 = employee1;
hs.add(employee1);
hs.add(employee2);

System.out.println(hs.size());

The output will be 1. So that means one of the item was not unique.

Read the link I have posted for more clarity and if you have any doubts, ask in comments.

Community
  • 1
  • 1
Jitin Kodian
  • 371
  • 3
  • 13