[!NOTE] Sometimes you don't care about order, and you don't even care about key-value mapping. You just have a massive influx of data and you need to mathematically guarantee that every single item is completely unique.
If you use an ArrayList, adding "BMW" ten times will add ten distinct "BMW" entries into the list.
If you use a HashSet, it evaluates the data, realizes "BMW" already exists, and silently ignores the other nine additions.
Use Cases
Imagine you are parsing a text file containing 10,000 email addresses, trying to build a spam-marketing list. If John signed up 5 times, you don't want to email him 5 times. By dumping the entire list into a HashSet, all duplicates are automatically stripped away in milliseconds!
Syntax
The syntax is identical to an ArrayList, but with the magical uniqueness guarantee natively built-in.
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW"); // Ignored!
cars.add("Mazda");
cars.add("Volvo"); // Ignored!
// Outputs 4, not 6!
System.out.println("Total Unique Cars: " + cars.size());
// HashSets use .contains() which is incredibly fast!
if(cars.contains("Mazda")) {
System.out.println("Mazda is in the unique Set.");
}
}
}
[!CAUTION] No Concept of Indexing! A
HashSetdoes not use indices. You cannot ask a HashSet for.get(2). The items are mathematically scattered in memory, and the "order" they are stored in is basically random. If you iterate through a HashSet, do not expect it to print in the order you added the items!
HashSet for Fast Membership Checks
A HashSet is excellent when you only need to know whether something exists. It gives fast contains() checks and automatically prevents duplicates.
Removing Duplicates
List<String> emails = List.of(
"a@example.com",
"b@example.com",
"a@example.com"
);
Set<String> uniqueEmails = new HashSet<>(emails);
System.out.println(uniqueEmails.size()); // 2
Visited Tracking
Set<String> visitedPages = new HashSet<>();
if (!visitedPages.contains("/home")) {
visitedPages.add("/home");
System.out.println("First visit");
}
Common Mistakes
- Expecting items to come out in insertion order.
- Trying to retrieve by index.
- Using a set when duplicate counts matter.
- Using custom objects without correct
equals()andhashCode().
Mini Practice
Given a list of usernames with duplicates, print only unique usernames. Then check whether a specific username has already appeared.
Practice Lab: Unique Username Filter
Use HashSet when uniqueness matters more than order.
- Create a list of usernames with duplicates.
- Add each username into a
HashSet<String>. - Print the unique usernames.
- Check whether a username already exists before adding.
- Try printing multiple times and notice that order is not the main promise.
Goal: Use sets for uniqueness and fast membership checks.
Revision Checkpoint
HashSet: Stores unique values.- No index: You cannot retrieve by position.
contains: Fast membership check.- Order: Plain
HashSetdoes not promise insertion order. - Use case: Deduplication and visited tracking.
Before the quiz: Decide whether you need uniqueness only or duplicate counts too.