views:

177

answers:

3

As we know two Set instances are equal iff they contain the same elements, BUT is it possible to have the same element in two different sets (Set interface can not contain duplicate element)?

+1  A: 

the equals method is described pretty well here.

Victor
+2  A: 

Distinct sets have no affect on one another. Set A can contain '123456', and Set B can also contain '123456' - the prohibition on duplicates is for a single instance, not across instances.

Equality for two sets implies that their contents are identical.

Visage
There are all sorts of problems that come up with defining "equality". I believe the java.uti.set subclasses all use the objects .equals method to check if one object is equal to another. Another way to define this is to use the == operator in Java, which is what I believe the .equals method defaults to unless it is overwritten. There are quite a few subtleties with sets to consider without even going into Bertrand Russels set theory paradox.
ldog
+2  A: 

You are not comparing the elements inside a Set with each other to detect if they are equal, you are comparing the Elements of Set1 with the elements of Set2.

Markus Lausberg