← Back to Home

HashSet

HashSet is one of the most commonly used implementations of the Set interface in the Java Collections Framework. It stores unique elements and provides fast performance for basic operations using hashing. This is a very high-frequency interview topic.

What Is HashSet?

  • Implementation of the Set interface
  • Stores unique elements only
  • Does not maintain insertion order
  • Allows at most one null
  • Part of java.util package
Set<String> set = new HashSet<>();
          

Position in Collection Hierarchy

Iterable
   └── Collection
        └── Set
             └── HashSet
          

How HashSet Works Internally (Interview Favorite)

  • Backed by a HashMap internally
  • Elements stored as keys
  • Dummy object used as value
private static final Object PRESENT = new Object();
          
  • ✔ Fast lookup
  • ✔ No duplicate keys allowed

Key Characteristics of HashSet

  • Unordered (no guarantee of order)
  • Average time complexity O(1)
  • Uses hashCode() and equals()
  • Not synchronized (not thread-safe)

Creating a HashSet

HashSet<Integer> set = new HashSet<>();
          

With Initial Capacity & Load Factor

HashSet<Integer> set = new HashSet<>(16, 0.75f);
          
  • Default capacity = 16
  • Default load factor = 0.75

Commonly Used Methods

set.add("Java");
set.remove("Java");
set.contains("Java");
set.size();
set.isEmpty();
          

Duplicate Handling Example

HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Java");

System.out.println(set); // [Java]
          
  • ✔ Duplicate ignored
  • ✔ No exception thrown

Importance of equals() and hashCode() (Critical)

HashSet uniqueness depends on:

  • hashCode() → bucket location
  • equals() → equality check
class Employee {
    int id;
}
          

⚠️ Without overriding both methods, duplicates may be stored logically.

Null Handling

  • Allows one null element
set.add(null);
set.add(null); // ignored
          

Performance Complexity

Operation Time Complexity
add O(1) average
remove O(1)
contains O(1)
iteration O(n)

HashSet vs LinkedHashSet vs TreeSet

Feature HashSet LinkedHashSet TreeSet
Order No Insertion Sorted
Performance Fastest Slightly slower Slowest
Null allowed One One ❌ No
Internal DS HashMap LinkedHashMap Red-Black Tree

Thread Safety

  • Not thread-safe

Thread-safe Alternatives: Collections.synchronizedSet(), CopyOnWriteArraySet

When to Use HashSet

  • Need unique elements
  • Fast lookup required
  • Order does not matter
  • Membership testing

Common Beginner Mistakes

  • Expecting insertion order
  • Forgetting to override equals() and hashCode()
  • Using mutable objects as elements
  • Using HashSet in concurrent scenarios without synchronization

Interview-Ready Answers

Short Answer

HashSet is a Set implementation that stores unique elements using hashing.

Detailed Answer

In Java, HashSet implements the Set interface and uses a HashMap internally to store elements as keys. It ensures uniqueness using hashCode() and equals(), provides constant-time performance for basic operations, and does not maintain insertion order.

Key Takeaway

Use HashSet when uniqueness and performance matter more than order. Proper implementation of equals() and hashCode() is essential for correctness.