A HashSet in Java is a part of the Java Collections Framework and is used to store unique elements. It internally uses a HashMap to store data, ensuring that duplicate elements are not allowed.

Definition

A HashSet is a collection that uses a hash table for storage. It implements the Set interface and uses hashing to manage and retrieve data quickly. Since it’s backed by a HashMap, each element in the HashSet is stored as a key in the underlying HashMap with a dummy value. This approach guarantees that each element in the HashSet is unique.

Internal Working of HashSet

  • When an object is added to the HashSet, its hash code is computed by calling its hashCode() method.
  • The hash code determines the bucket index where the object will be stored in an internal array called the bucket array.
  • If multiple elements have the same hash code, they are stored in a linked list or a binary tree within the same bucket (based on the Java version).
  • When a new element is added, the HashSet checks if the element is already present by comparing the hash code and then using the equals() method.

Example:

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        // Create a HashSet to store integers
        HashSet<Integer> set = new HashSet<>();
        
        // Add elements to the HashSet
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(20);  // Duplicate element, will be ignored
        
        // Print the HashSet
        System.out.println("HashSet: " + set);  // The order of elements is not guaranteed
        
        // Check if a specific element exists
        System.out.println("Contains 20? " + set.contains(20));  // Expected: true
        System.out.println("Contains 40? " + set.contains(40));  // Expected: false
        
        // Remove an element
        set.remove(30);
        System.out.println("HashSet after removal: " + set);
        
        // Iterate over the elements of the HashSet
        System.out.println("Iterating over the HashSet:");
        for (Integer element : set) {
            System.out.println(element);
        }
    }
}

Output:

HashSet: [10, 20, 30]
Contains 20? true
Contains 40? false
HashSet after removal: [10, 20]
Iterating over the HashSet:
10
20

Features of HashSet

  • HashSet ensures that no duplicate values are stored.
  • It does not guarantee the order of elements.
  • It allows one null element.
  • HashSet is not thread-safe by default.
  • HashSet resizes its underlying array dynamically as the number of elements grows.

Advantages of HashSet

  • Hashing provides constant-time performance for basic operations like add, remove, and contains.
  • It’s an ideal choice for storing large datasets where duplicate elements are not needed.
  • Provides a straightforward way to enforce uniqueness in collections.