code

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

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

python sets