LinkedHashSet
About
LinkedHashSet
is a part of the Java Collections Framework that combines the unique element storage of HashSet
with predictable iteration order. It maintains a doubly-linked list running through all its entries, which preserves the order in which elements were inserted.
Features
Preserves Insertion Order: Unlike
HashSet
, the iteration order of elements in aLinkedHashSet
is predictable and matches the insertion order.No Duplicates: Ensures all elements are unique.
Null Element: Allows one
null
element.Backed by
LinkedHashMap
: Internally uses aLinkedHashMap
to store elements.Non-Synchronized: Not thread-safe. External synchronization is required for concurrent access.
Efficient Performance: Provides O(1) time complexity for basic operations like add, remove, and contains.
Internal Working
1. LinkedHashMap as the Backbone
LinkedHashSet
internally uses aLinkedHashMap
where elements are stored as keys, and a constant dummy object (PRESENT
) is stored as values.
2. Maintaining Insertion Order
The
LinkedHashMap
maintains a doubly-linked list that connects all entries in the map in their insertion order.This doubly-linked list ensures that the iteration order is consistent with the insertion order.
3. Adding Elements
When an element is added using the
add()
method, theput()
method of the underlyingLinkedHashMap
is called.If the element is already present, it is ignored, ensuring uniqueness.
4. Removing Elements
The
remove()
method removes the key-value pair from the underlyingLinkedHashMap
, which also updates the doubly-linked list.
5. Iteration
The iterator traverses the doubly-linked list maintained by the
LinkedHashMap
, ensuring the order of elements matches their insertion order.
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 in insertion order.
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
Not Thread-Safe: Requires external synchronization for concurrent access.
Memory Overhead: Higher memory usage than
HashSet
due to the maintenance of a doubly-linked list.Dependent on Hashing: Relies on well-implemented
hashCode()
andequals()
methods for element uniqueness.
Real-World Usage
Preserving Order: Scenarios where the order of elements matters, such as caching or building ordered collections.
Unique Collections: Storing unique elements while maintaining insertion order.
Deterministic Iteration: Applications requiring predictable iteration order, like serializing data.
Examples
1. Basic Operations
2. Iteration
3. Preserving Order
4. Using Collections.synchronizedSet()
Collections.synchronizedSet()
Last updated
Was this helpful?