HashSet: Unique Elements Without Duplicates
Introduction
A Set contains no duplicate elements (by equals / hashCode contract). HashSet is the default hash-based implementation—fast add, remove, and contains. Use it for deduplication, tags, and membership tests.
Prerequisites
- Collections Overview
- HashMap (HashSet is backed by a HashMap internally)
What Is a Set
import java.util.HashSet;
import java.util.Set;
Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("collections");
tags.add("java"); // duplicate ignored
System.out.println(tags); // order not guaranteed
System.out.println(tags.size()); // 21) Create a HashSet
Set<Integer> numbers = new HashSet<>();
Set<Integer> fromList = new HashSet<>(List.of(1, 2, 2, 3));2) Core Operations
numbers.add(10);
numbers.add(10); // no effect
boolean has = numbers.contains(10);
numbers.remove(10);
numbers.removeIf(n -> n < 0);
numbers.clear();
int size = numbers.size();3) Traverse
for (int n : numbers) {
System.out.println(n);
}
numbers.forEach(System.out::println);No index access—sets are not lists.
4) Set Operations (Useful Patterns)
Set<Integer> a = new HashSet<>(Set.of(1, 2, 3));
Set<Integer> b = new HashSet<>(Set.of(3, 4, 5));
Set<Integer> union = new HashSet<>(a);
union.addAll(b);
Set<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b);
Set<Integer> difference = new HashSet<>(a);
difference.removeAll(b);5) Deduplicate a List
import java.util.ArrayList;
import java.util.List;
List<Integer> scores = List.of(95, 88, 95, 76, 88);
Set<Integer> unique = new HashSet<>(scores);
List<Integer> uniqueList = new ArrayList<>(unique);Order is lost with HashSet—use LinkedHashSet to preserve insertion order if needed.
6) HashSet vs HashMap
Internally HashSet stores elements as keys in a HashMap with dummy values. Same hashCode/equals rules apply.
7) HashSet vs TreeSet
HashSet | TreeSet | |
|---|---|---|
| Order | none guaranteed | sorted |
| Speed | faster average | O(log n) |
| Null | one null element allowed | no null (generally) |
Real Mini Example: Unique Visitors
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class UniqueVisitors {
public static void main(String[] args) {
Set<String> visitors = new HashSet<>();
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter names (empty line to finish):");
while (true) {
String name = scanner.nextLine().strip();
if (name.isEmpty()) break;
if (visitors.add(name)) {
System.out.println("Welcome, " + name + "!");
} else {
System.out.println(name + " already checked in.");
}
}
System.out.println("Unique visitors: " + visitors.size());
}
}
}add returns false if element already present.
Common Beginner Mistakes
Expecting Stable Iteration Order
Use LinkedHashSet or sort a copy list for display.
Relying on equals Not Implemented
Custom objects need consistent equals and hashCode (Object Class).
Using Set When List Is Needed
Sets reject duplicates—use List when order and duplicates matter.
Mini Practice
From List<String> with duplicate emails, print unique emails sorted (copy to list, then Collections.sort).
What’s Next
Exercise: Deduplicate and Sort Scores.
FAQ
Can I sort a HashSet directly?
Not in place. Copy to ArrayList and sort, or use TreeSet.
How does HashSet remove duplicates?
add checks existing elements via hash bucket and equals.
Is HashSet thread-safe?
No. Use Collections.synchronizedSet or ConcurrentHashMap.newKeySet() when needed.
Empty set?
Set.of() or new HashSet<>().
Primitive sets?
Use Set<Integer> with boxing, or third-party primitive sets (advanced).