**Posted By:**
Safder_Meah

**Posted On:**
Sunday, October 22, 2006 10:28 AM

Hi everyone, I have this question regarding the use of the add method in Set interfaces. Here's wht the api states "adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e))" Now lets say i have a test class with two values a and b. Now, let's suppose that two objects of this class would be considered equal, if and only if both the values of a and b are equal in both the instances. So in order to implement this I overide the equals method of the test class to do this. Now if i have two objects of this class with equal values, and I try to add this to the set, I would think that the second instance would not be added,

More>>
Hi everyone,

I have this question regarding the use of the add method in Set interfaces. Here's wht the api states

"adds the specified element, o, to this set if this set contains no element e such that (o==null ? e==null : o.equals(e))"

Now lets say i have a test class with two values a and b. Now, let's suppose that two objects of this class would be considered equal, if and only if both the values of a and b are equal in both the instances. So in order to implement this I overide the equals method of the test class to do this.

Now if i have two objects of this class with equal values, and I try to add this to the set, I would think that the second instance would not be added, since the instances in my case should be equal, but in actuality, both the instances are added to the set. (by the way, the underlying implementing class is a HashSet)

I am not sure, whether I got the definition right, so I'd appreciate it, if someone could explain me how'd I be able to overide the equals method in this case.

ps: the same question applies to the "contains" method of the set interface too i.e. how can you overide the behavior of the equals method in tht case too..

Thank you...

<<Less