# Sets

{% hint style="warning" %}
This page is from my original notes and is not up to the latest quality standards. Read with care or [help make it better!](https://github.com/64bitpandas/cs61b-notes/pulls)
{% endhint %}

## Basics

A Set stores a collection of values with **no duplicates.** Sets have no inherent order, so you can't rely on expecting any value to come before any other value when iterating through them.

Some set functions include:

* `add(T x)`
* `contains(T x)`
* `size()`

## ArraySet

An ArraySet is an array-based solution to a set implementation.&#x20;

* Objects get added to an array that gets [resized](https://cs61b.bencuan.me/asymptotics/amortization) when it's too full.
* In order to allow for iteration, we can use one of two methods:
  * One method is to use **iterators** which work very similarly to Python iterators:

    ```java
    Iterator<Integer> seer = set.iterator();
    while (seer.hasNext()) {
      System.out.println(seer.next());
    }
    ```
  * Another method is to implement the `Iterator` and `Iterable` interface.
    * Iterator must implement `hasNext()` and `next()` methods
    * Requires generic type
    * Iterable must implement `iterator()` method which returns the Iterable object
    * Allows usage of for/foreach loops
