Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Set Duplicates

 
vimal kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm adding the Employee objects in the Set collection. I have overrided the equals, hashCode and toString method of the Employee class. If there are two Employee instances with the same name, it won't be added to the Set since it won't allow duplicates. The output of the below program is [vimal] as expected.

However, if we replace the equals method of the Employee class as follows where the Employee class parameter is replaced by Object class parameter, which is a correct override, it results in output [vimal, vimal].

Replaced equals method:

public boolean equals(Employee x)
{

if(empname. equals(x.empname))

return true;
else
return false;
}




I don't know the reason behind this output.

import java.util.Set;
import java.util.HashSet;
class SetDemo
{
public static void main(String[] a)
{
Employee e = new Employee("vimal");
Employee e2 = new Employee("vimal");

Set ll = new HashSet();

ll.add(e);
ll.add(e2);

System.out.println(ll);
}

}

class Employee
{
String empname;

Employee(String x)
{
empname = x;
}
public String toString()
{
return empname;

}
public int hashCode()
{
return empname.length();

}
public boolean equals(Object x)
{

if(x instanceof Employee && empname. equals(((Employee)x).empname))

return true;
else
return false;
}


}
 
Sucharitha Lakshmi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

When you say replace with "the code that takes parameter as Employee"
its actually not overriding of equals(). It is overloading.

And when you add objects to set,
In case 1: valid overriding, with Object as parameter, it returns true.So, the Set doesn't allow duplicate insertion.
In case 2: you are not overriding, means the Object class' equals() method is called which returns false because the objects are different(two objects having same employee name). So, both are added to Set.

Regards
Sucharitha.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, if we replace the equals method of the Employee class as follows where the Employee class parameter is replaced by Object class parameter, which is a correct override, it results in output [vimal, vimal].


A correct override? You have overloaded the equals method. Your method does not get called. It's using Object's equals because you have not overridden it.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vimal,
Welcome to javaranch.
The classes implementing the Set interface compares the objects using

method.
Look closely in the first example where you are getting the expected result you are overriding the Object's equals method and in the second case when you are replacing the equal method you are infact overloading the equal method not overriding it.
Please make a proper identation of your code using code tag.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

vimal kumar wrote:

However, if we replace the equals method of the Employee class as follows where the Employee class parameter is replaced by Object class parameter, which is a correct override, it results in output [vimal, vimal].


I think you must have mixed it up.
The correct override


produces only one vimal wheras the non-override

produces two vimals, as equals of Object is not overridden and returns false if the references are unequal.



Yours,
Bu.
 
vimal kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case,

e.equals(e2) should return false if Employee parameter is used in the equals method parameter but it returns true.


You can override the super method with argument of type super with the method of argument sub type. That's what done in the above.

In case of where object is required, override it with the sub type Employee
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cowgirls,

Vimal wrote:
You can override the super method with argument of type super with the method of argument sub type. That's what done in the above.
No, you are mixing up covariant returns with parameters. The following is no override and hence does not compile in java 5:


class A {
@Override // no compile
public boolean equals (A a) {
return false;
}
}


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic