100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

HashSet in Java

Understand how HashSet guarantees uniqueness by wrapping a HashMap internally, with average O(1) add/contains/remove.

Collections FrameworkBeginner7 min readJul 7, 2026
Analogies

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

text
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

java
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

text
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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#HashSetInJava#HashSet#Syntax#Explanation#Example#StudyNotes#SkillVeris