LinkedHashSet<E> is a subclass of HashSet that maintains the insertion order of elements. Unlike HashSet, which does not guarantee any order, LinkedHashSet preserves the order in which elements were inserted.
1. Key Features of LinkedHashSet
✅ No duplicate elements (like any Set).
✅ Maintains insertion order (Unlike HashSet, which is unordered).
✅ Uses a HashTable + Doubly Linked List internally to store elements.
✅ O(1) average time complexity for add, remove, and contains operations (like HashSet).
2. Internal Working of LinkedHashSet
LinkedHashSetextendsHashSetand internally uses a LinkedHashMap.- It maintains a linked list of elements in insertion order.
- This makes it slightly slower than
HashSet(due to extra memory for maintaining order).
3. Example Usage of LinkedHashSet
Example 1: Basic Usage
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> names = new LinkedHashSet<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
names.add("Bob"); // Duplicate, won't be added
System.out.println(names); // Output: [Alice, Bob, Charlie] (Insertion Order Maintained)
}
}
- Duplicates are ignored (
Bobis not added twice). - Insertion order is preserved (
Alice → Bob → Charlie).
Example 2: Difference Between HashSet and LinkedHashSet
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("Banana");
hashSet.add("Apple");
hashSet.add("Mango");
System.out.println("HashSet: " + hashSet);
// Output: [Apple, Mango, Banana] (Unordered)
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Banana");
linkedHashSet.add("Apple");
linkedHashSet.add("Mango");
System.out.println("LinkedHashSet: " + linkedHashSet);
// Output: [Banana, Apple, Mango] (Maintains Insertion Order)
}
}
HashSetdoes not maintain order.LinkedHashSetkeeps elements in insertion order.
4. Performance Comparison
| Feature | HashSet | LinkedHashSet | TreeSet |
|---|---|---|---|
| Ordering | Unordered | Insertion Order | Sorted Order |
| Duplicates | ❌ No | ❌ No | ❌ No |
| Performance (Add/Delete/Contains) | O(1) | O(1) (Slightly slower due to linked list) | O(log n) |
| Uses | Hash Table | Hash Table + Linked List | Red-Black Tree |
| Memory Usage | Lower | Higher (due to Linked List) | Higher |
5. When to Use LinkedHashSet?
✅ Use LinkedHashSet if:
- You need to maintain insertion order.
- You need fast lookup (O(1)) but also want ordered elements.
- You don’t need sorted order (use
TreeSetfor that).
❌ Avoid LinkedHashSet if:
- Order doesn’t matter (Use
HashSetfor better performance). - You need elements sorted (Use
TreeSetinstead).
Final Thoughts
LinkedHashSetis likeHashSetbut preserves insertion order.- It uses a Linked List + Hash Table internally.
- Faster than
TreeSetbut slightly slower thanHashSet. - Ideal when you need unique elements in the order they were added.