Sets
This page is from my original notes and is not up to the latest quality standards. Read with care or help make it better!
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.
Objects get added to an array that gets resized 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:
Another method is to implement the
Iterator
andIterable
interface.Iterator must implement
hasNext()
andnext()
methodsRequires generic type
Iterable must implement
iterator()
method which returns the Iterable objectAllows usage of for/foreach loops
Last updated