1. Introduction
HashSet is an implementation of the Set interface that stores unique elements with no duplicates. It is one of the most efficient Set implementations for general-purpose use when ordering doesn't matter.
Cricket analogy: A squad roster that automatically rejects listing the same player twice, without caring about batting order, works like HashSet enforcing uniqueness without preserving any particular sequence.
Internally, HashSet is backed by a HashMap: every element you add becomes a key in an internal HashMap, mapped to a constant dummy value (a static Object). This is why HashSet inherits HashMap's average O(1) performance characteristics.
Cricket analogy: HashSet is like a scorer's tally sheet that's secretly just a HashMap where every player's name is the key and the value is always a fixed 'played' checkmark, inheriting the same average O(1) lookup speed.
2. Syntax
Set<String> set = new HashSet<>();
set.add("apple");
set.add("apple"); // ignored, duplicate
set.contains("apple");
set.remove("apple");
set.size();3. Explanation
Because HashSet delegates to an internal HashMap (element -> PRESENT constant), add(), contains(), and remove() all run in average O(1) time — the same as HashMap's put(), containsKey(), and remove(). Duplicate elements (as determined by equals() and hashCode()) are automatically ignored.
Cricket analogy: Adding the same player to a HashSet-backed squad list twice is silently ignored because equals()/hashCode() recognize the duplicate, and checking if a player is already selected runs in average O(1) time, just like HashMap's containsKey().
Like HashMap, HashSet does not guarantee any iteration order, and it is not synchronized. If you need insertion-order iteration, use LinkedHashSet; if you need sorted order, use TreeSet.
Cricket analogy: A HashSet-based list of squad members prints names in no fixed order, but switching to a LinkedHashSet-style rotation preserves selection order, and a TreeSet-style rotation keeps them alphabetically sorted like a scoreboard.
Exam trap: HashSet is NOT backed by an array or a tree directly — it's backed by a HashMap under the hood. Confusing this with TreeSet (backed by a TreeMap/Red-Black tree) is a common mistake.
4. Example
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
Set<String> languages = new HashSet<>();
languages.add("Java");
languages.add("Python");
languages.add("Java"); // duplicate, ignored
System.out.println("Set: " + languages);
System.out.println("Size: " + languages.size());
System.out.println("Contains 'Python': " + languages.contains("Python"));
languages.remove("Python");
System.out.println("After remove: " + languages);
}
}5. Output
Set: [Java, Python]
Size: 2
Contains 'Python': true
After remove: [Java]6. Key Takeaways
- HashSet is internally backed by a HashMap (elements are keys, value is a dummy constant).
- add(), contains(), and remove() are average O(1).
- Duplicate elements are silently ignored.
- Iteration order is not guaranteed.
- Use LinkedHashSet for insertion order or TreeSet for sorted order.
Practice what you learned
1. What data structure backs a HashSet internally?
2. What is the average time complexity of HashSet.add()?
3. What happens when you add a duplicate element to a HashSet?
4. Which Set implementation should you use if you need sorted order?
5. Does HashSet guarantee insertion order iteration?
Was this page helpful?
You May Also Like
HashMap in Java
Learn how HashMap stores key-value pairs using hashing, its null-key rules, and why iteration order is not guaranteed.
TreeMap in Java
Learn how TreeMap keeps keys sorted using a Red-Black tree, offering O(log n) operations in exchange for guaranteed ordering.
Iterator in Java
Learn how to safely traverse and modify collections using Iterator, and why direct modification during a for-each loop throws ConcurrentModificationException.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics