The add method of the Set interface
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

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

Re: The add method of the Set interface

Posted By:   Robert_Lybarger  
Posted On:   Sunday, October 22, 2006 02:58 PM

The English translation is that Set implementations will allow one null entry and no duplicate non-null entries. The exact definition of whether two entries duplicate each other depends entirely on their implementation of "equals()". The simplest and most naive check is to see if the references themselves (not the underlying values) are equal -- that is, if (x==y). What you likely want is to check whether x.a==y.a and then whether x.b==y.b (etc). As a further consideration, Hash-based collections also depend on hashCode being correctly implemented consistent to the behavior of equals implementation. If you override one and not the other, you might get some interesting results. Try a google search for 'java equals hashcode' for some reading. (This is one of the more important concepts that the java cert exam will beat you up over.)
About | Sitemap | Contact