Skip to main content
  1. /classes/
  2. Classes, Fall 2025/
  3. CS 2381 Fall 2025: Course Site/

cs2381 Notes: 10-03 Intro to Sets

·355 words·2 mins·

Sets

A set is an unordered collection of unique items.

Mathematically, sets can contain any appropriate mathematical objects.

Programmatically, sets are a generic type - we could have a set of Strings or a set of Animals. Logically, the items should be immutable values with well defined equality.

A = {1, 2, 3}
B = {"apple", "pear", "grape"}

Mathemtically, sets have the following operations:

  • contains?(item) - Does the set contain this item?
  • union(set) - All items in either set.
  • intersection(set) - All items occurring in both sets.
  • subset?(setB) - Does this set contain all items in setB?
  • superset?(setB) - Does setB contain all items in this?

The Java standard library has a Set interface, missing most of those methods: Java17 Set

You can use the Java Set methods to build the mathematical set methods easily enough, but that’s still silly, so we’ll talk mostly about our own Set interfaces.

(examples)

Programatically, sets generally have these operations:

  • add
  • remove
  • size

And then we have the question of whether a set is an immutable value or a mutable object.

If you want to sensibly have a set of sets, then immutable values make more sense.

interface Set<T> {
    Set<T> add(T item);
    Set<T> remove(T item);
    
    Set<T> union(Set<T> other);
    Set<T> intersection(Set<T> other);
    
    boolean contains(T item);
    boolean subset(Set<T> other);
    boolean superset(Set<T> other);
    
    int size();
}

With an ConsList or unsorted ArrayList, every simple operation is O(n) and operations that require comparing entire sets may be even more expensive.

Now let’s consider implementing this interface with a sorted ArrayList.

  • Add is O(n)
  • Remove is O(n)
  • Union and intersection can both be done in O(n)
  • Contains is O(n)… unless we do binary search, then it’s O(log n)
  • Superset and subset are O(n)
  • Size is O(1)

Mutable sets?

interface MutableSet<T> {
    void add(T item);
    void remove(T item);
    
    Set<T> union(Set<T> other);
    Set<T> intersection(Set<T> other);
    
    boolean contains(T item);
    boolean subset(Set<T> other);
    boolean superset(Set<T> other);
    
    int size();
}
  • Lists are immutable, so there’s no benefit.
  • Arrays are mutable… but for a sorted array there’s no benefit either.
  • So we’ll ignore this for now, but this may end up being useful at some point in the future.
Nat Tuck
Author
Nat Tuck