aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Set Duplicates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Set Duplicates" Watch "Set Duplicates" New topic
Author

Set Duplicates

vimal kumar
Greenhorn

Joined: Aug 16, 2006
Posts: 6
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

Joined: May 04, 2006
Posts: 2
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
vimal kumar
Greenhorn

Joined: Aug 16, 2006
Posts: 6
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

Joined: Aug 25, 2006
Posts: 1274
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set Duplicates