Java Reference ยท Collections
The Set interface
Reference for java.util.Set: collections without duplicates, add() return values, HashSet versus LinkedHashSet versus TreeSet, Set.of() and equality rules.
java.util.Set<E> is a collection that cannot contain duplicate elements. Adding an element that is already present does nothing and add() returns false. Membership tests are what sets are built for, and the common implementations answer contains() in constant time (HashSet) or logarithmic time (TreeSet).
Set adds no new methods to Collection, but it tightens their contracts: no duplicates, and equals() compares sets by content regardless of order. Whether iteration order is defined depends on the implementation: HashSet makes no promise, LinkedHashSet keeps insertion order and TreeSet keeps sorted order.
Set<String> seen = new HashSet<>(); if (!seen.add(word)) { /* duplicate */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface Set<E> extends Collection<E>
| Member | Description |
|---|---|
boolean add(E e) |
Adds e if absent; returns true if the set changed. |
boolean remove(Object o) |
Removes o if present. |
boolean contains(Object o) |
Membership test. |
addAll, retainAll, removeAll, containsAll |
Union, intersection, difference and subset tests. |
static <E> Set<E> of(E... elements) |
Immutable set (Java 9); duplicates throw IllegalArgumentException. |
static <E> Set<E> copyOf(Collection<? extends E> c) |
Immutable copy (Java 10); duplicates are ignored. |
Throws: UnsupportedOperationException on immutable sets; NullPointerException for null in sets that forbid it (Set.of(), TreeSet with natural order); ClassCastException for elements that cannot be compared.
Usage notes
- Equality:
HashSetusesequals()/hashCode();TreeSetusescompareTo()/the comparator. A comparator that treats two different objects as equal makesTreeSetdrop one of them. - Mutable elements: changing an element's fields after insertion can make it unfindable in a
HashSet. Use immutable elements. - Order: never depend on
HashSetorSet.of()iteration order; it can differ between runs and Java versions. null:HashSetandLinkedHashSetaccept onenull;TreeSetwith natural ordering andSet.of()reject it.- Set algebra with bulk methods is shown on Set operations.
- No indexed access: convert to a list when you need positions.
- Thread safety: none by default; see
ConcurrentHashMap.newKeySet()andCopyOnWriteArraySet.
Related
- HashSet, LinkedHashSet, TreeSet, Set operations, Collection
- Lessons: HashSet, TreeMap and TreeSet