26. Set API
Table of Contents
- 26.1 Set Hierarchy Java-Collections-Framework
- 26.2 Characteristics of Each Set Implementation
- 26.3 Equality Rules in Sets
- 26.4 Creating Set Instances
- 26.5 Main Operations on Sets
- 26.6 Common Pitfalls
- 26.7 Summary Table
A Set in Java represents a collection that contains no duplicate elements.
It models the mathematical concept of a set: unordered (unless using an ordered implementation) and composed of unique values.
All Set implementations rely on equality semantics (either equals() or comparator logic.
26.1 Set Hierarchy (Java Collections Framework)
Set<E>
├── SequencedSet<E> (Java 21+)
│ └── LinkedHashSet<E> (ordered)
├── HashSet<E> (unordered)
└── SortedSet<E>
└── NavigableSet<E>
└── TreeSet<E> (sorted)
All Set implementations require:
- uniqueness of elements
- predictable equality and hashing (depending on implementation)
Note
LinkedHashSet is now formally a SequencedSet since Java 21.
26.2 Characteristics of Each Set Implementation
26.2.1 HashSet
- Fastest general-purpose Set
- Unordered (no iteration order guarantee)
- Uses
hashCode()andequals() - Allows one
nullelement
Set<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("A"); // duplicate ignored
System.out.println(set); // order not guaranteed
26.2.2 LinkedHashSet
- Maintains insertion order
- Slightly slower than HashSet
- Useful when predictable iteration order is required
Set<String> set = new LinkedHashSet<>();
set.add("A");
set.add("C");
set.add("B");
System.out.println(set); // [A, C, B]
26.2.3 TreeSet
A sorted Set whose order is determined by:
- Natural ordering (
Comparable) - A provided
Comparator
TreeSet:
- No
nullelements allowed (NullPointerException at runtime) - Guarantees sorted iteration
- Supports range views:
headSet(),tailSet(),subSet()
TreeSet<Integer> tree = new TreeSet<>();
tree.add(10);
tree.add(1);
tree.add(5);
System.out.println(tree); // [1, 5, 10]
Note
TreeSet requires all elements to be mutually comparable — mixing non-comparable types produces ClassCastException.
Operations (add, remove, contains) are O(log n).
26.3 Equality Rules in Sets
The rules differ depending on implementation.
26.3.1 HashSet & LinkedHashSet
Uniqueness is determined by two methods:
hashCode()equals()
Two objects are considered the same element if:
- Their hash codes match
- Their
equals()method returnstrue
Warning
If you mutate an object after adding it to a HashSet or LinkedHashSet, its hashCode may change and the set may lose track of it.
26.3.2 TreeSet
Uniqueness is based on compareTo() or the provided Comparator.
If compare(a, b) == 0 then the objects are considered duplicates, even if equals() is false.
Comparator<String> comp = (a, b) -> a.length() - b.length();
Set<String> set = new TreeSet<>(comp);
set.add("Hi");
set.add("Yo"); // same length → treated as duplicate
System.out.println(set); // ["Hi"]
26.4 Creating Set Instances
26.4.1 Using Constructors
Set<String> s1 = new HashSet<>();
Set<String> s2 = new LinkedHashSet<>();
Set<String> s3 = new TreeSet<>();
26.4.2 Copy Constructors
List<String> list = List.of("A", "B", "C");
Set<String> copy = new HashSet<>(list); // order lost
System.out.println(copy);
Set<String> ordered = new LinkedHashSet<>(list); // maintains the order from the list
System.out.println(ordered);
26.4.3 Factory Methods
Set<String> s1 = Set.of("A", "B", "C"); // immutable
Set<String> empty = Set.of(); // empty immutable set
Note
Factory-created sets are immutable: adding or removing elements throws UnsupportedOperationException.
Set.of(...) rejects duplicates at creation time → IllegalArgumentException and rejects null → NullPointerException
26.5 Main Operations on Sets
26.5.1 Adding Elements
set.add("A"); // returns true if added
set.add("A"); // returns false if duplicate
26.5.2 Checking Membership
set.contains("A");
26.5.3 Removing Elements
set.remove("A");
set.clear();
26.5.4 Bulk Operations
set.addAll(otherSet);
set.removeAll(otherSet);
set.retainAll(otherSet); // intersection
26.6 Common Pitfalls
- Using TreeSet with non-comparable objects →
ClassCastException - TreeSet does not use
equals()at all: only comparator/compareTo decides uniqueness. - Using mutable objects as Set keys → breaks hashing rules
- Factory Set.of() is immutable — modification fails
- HashSet does not guarantee iteration order
- TreeSet treats objects with compare()==0 as duplicates even if not equal
26.7 Summary Table
| Implementation | Keeps Order? | Allows Null? | Sorted? | Underlying Logic |
|---|---|---|---|---|
| HashSet | No | Yes (1 null) | No | hashCode + equals |
| LinkedHashSet | Yes (insertion order) | Yes (1 null) | No | hash table + linked list |
| TreeSet | Yes (sorted) | No | Yes (natural/comparator) | compareTo / Comparator |