HashSet
About
HashSet
is a part of the Java Collections Framework and is implemented using a HashMap
internally. It represents a collection of unique elements and does not allow duplicate entries. HashSet
relies on the hashing mechanism for storing and retrieving elements efficiently.
Features
No Duplicates: Ensures all elements are unique. Duplicate entries are ignored.
Null Element: Allows a single
null
element.Unordered: Does not maintain insertion order; the order is determined by the hash table.
High Performance: Provides constant-time performance for basic operations like add, remove, and contains (O(1)), assuming the hash function disperses elements properly.
Not Thread-Safe:
HashSet
is not synchronized. For thread safety, you need to useCollections.synchronizedSet()
or considerConcurrentHashSet
alternatives.Backed by
HashMap
: Internally uses aHashMap
with the elements as keys and a dummy value.
Internal Working
1. HashMap as the Backbone
HashSet
internally uses aHashMap
to store its elements.Each element in the
HashSet
is stored as a key in the underlyingHashMap
, and the value is a constant dummy object (PRESENT
).
2. Adding Elements
When an element is added using
add(E e)
,HashSet
calls theput()
method of the underlyingHashMap
.If the key (element) already exists, it is ignored, ensuring uniqueness.
3. Retrieving Elements
The
contains(Object o)
method uses thecontainsKey()
method of theHashMap
to check if the element exists.
4. Hashing and Buckets
The
HashSet
relies on thehashCode()
andequals()
methods to determine the uniqueness of elements.Elements are distributed across buckets based on their hash values.
Collisions are handled by storing multiple elements in a linked list or tree structure (depending on Java version).
5. Removing Elements
The
remove(Object o)
method calls theremove()
method of the underlyingHashMap
, which removes the key-value pair corresponding to the element.
Key Methods
Method
Description
add(E e)
Adds the specified element to the set if it is not already present.
remove(Object o)
Removes the specified element from the set if it exists.
contains(Object o)
Checks if the set contains the specified element.
size()
Returns the number of elements in the set.
clear()
Removes all elements from the set.
isEmpty()
Checks if the set is empty.
iterator()
Returns an iterator over the elements in the set.
toArray()
Returns an array containing all the elements in the set.
Big-O for Operations
Operation
Time Complexity
Add
O(1) (average case)
Remove
O(1) (average case)
Contains
O(1) (average case)
Iteration
O(n)
Limitations
No Ordering: The iteration order is not guaranteed.
Not Thread-Safe: Requires external synchronization for concurrent access.
Dependent on Hashing: Poorly implemented
hashCode()
andequals()
can lead to performance degradation.Memory Overhead: Requires extra memory due to the underlying
HashMap
.
Real-World Usage
Unique Collections: Maintaining a collection of unique elements, such as a list of unique IDs.
Membership Testing: Checking whether an element exists in a collection.
Set Operations: Performing mathematical set operations like union, intersection, and difference.
Examples
1. Basic Operations
2. Iteration
3. Set Operations
Last updated
Was this helpful?