HashSet and the Set Family Architectures
easyHashSetLinkedHashSetTreeSetPerformance
The Set interface defines a collection of unique elements. Most Java Set implementations are not built from scratch; they are Wrappers around existing Map implementations.
1. HashSet: The Wrapper Pattern
A HashSet is essentially a HashMap where:
- Keys = Your set elements.
- Values = A shared static constant dummy object called
PRESENT.
public boolean add(E e) {
return map.put(e, PRESENT) == null; // Uniqueness is enforced by map key uniqueness
}
2. Uniqueness Enforcement: Equals & HashCode
To successfully deduplicate elements, your class must override hashCode() and equals():
hashCode(): The bucket index. If this is different, the elements are distinct.equals(): If hashes collide, the set checks if they are equal.
3. The Set Family Comparison
| Implementation | Backing Map | Ordering | Performance |
|---|---|---|---|
HashSet |
HashMap |
None | $O(1)$ |
LinkedHashSet |
LinkedHashMap |
Insertion Order | $O(1)$ (higher memory) |
TreeSet |
TreeMap |
Sorted Order | $O(\log n)$ |
4. Selection Guide
- Use
HashSetby default for maximum speed. - Use
LinkedHashSetif you need to maintain the order in which elements were added (e.g., maintaining a history of unique visit tags). - Use
TreeSetonly when you need continuous sorted access to the elements (e.g., finding all values between "A" and "D").