Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Unlike other implementations of the Collection interface, implementations of the Set interface do not allow duplicate elements. This also means that a set can contain at most one null value. The Set interface does not define any new methods, and its add() and addAll() methods will not store duplicates. If an element is not currently in the set, two consecutive calls to the add() method to insert the element will first return true, then false. A Set models a mathematical set (see Table 15.3), that is, it is an unordered collection of distinct objects.
| Set Methods (a and b are sets) | Corresponding Mathematical Operations |
|---|---|
| a.containsAll(b) | b ⊆ a (subset) |
| a.addAll(b) | a = a ∪ b (union) |
| a.removeAll(b) | a = a − b (difference) |
| a.retainAll(b) | a = a ∩ b (intersection) |
| a.clear() | a = Ø (empty set) |