Set is a collection of distinct elements. By default sets are implemented as hash sets with hashCode method. It’s faster to find elements this way.

LinkedHashSet remembers the order in which elements are inserted with a linked list.

SortedSet can be iterated in sorted order. It is implemented as a red-black tree.

BitSet is an implementation of a set of nonnegative integers as a sequence of bits. The ith bit is 1 if i is present in the set. It’s efficient as long as the maximum number os not too large.

  • contains method checks whether a set contains a given value. subsetOf checks whether a set is a subset of another set.
  • union: can be written as | and ++.
  • intersect: can be written as &.
  • diff: can be written as &~ or --.