# Set operations

*406*pages on

this wiki

Sets have their own operations that other collections do not have. These are all methods of creating a new set out of two other sets.

**Union** - The set of items that are present in either set.

**Intersection** - The set of items that are present in both sets.

**Complement** - The set where all items in one set are removed from the other.

## Java Edit

these example assume the existance of two sets a and b

**Union**

public Set complement(Set a, Set b){ a.addAll(b); return a; }

**Intersection**

public Set Intersection(Set a, Set b){ a.retainAll(b); return a; }

**Complement**

public Set complement(Set a, Set b){ a.removeAll(b); return a; }

## Python Edit

- len(s) - cardinality of set s
- x in s - test x for membership in s
- x not in s - test x for non-membership in s
- s.issubset(t) - test whether every element in s is in t
- s.issuperset(t) - test whether every element in t is in s
- s.union(t) - new set with elements from both s and t
- s.intersection(t) - new set with elements common to s and t
- s.difference(t) - new set with elements in s but not in t
- s.symmetric_difference(t) - new set with elements in either s or t but not both