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:
Output:
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.