Clean • Professional
LinkedHashSet is a HashSet variant that maintains insertion order.
It is part of the java.util package and implements the Set interface.
A LinkedHashSet is a collection that:
Iterable
└── Collection
└── Set
└── LinkedHashSet
Implements:
LinkedHashSet internally uses a HashMap:
Internal Structure Diagram
Hash Table (Buckets) + Linked List
0 → [Java] → [Python] → [C++]
1 → [null]
2 → [HTML]
capacity × load factor.Use LinkedHashSet when:
Avoid if:
Default Constructor
Creates an empty LinkedHashSet with default capacity and load factor.
LinkedHashSet<String> set = new LinkedHashSet<>();
With Initial Capacity
Creates a LinkedHashSet with a specified initial capacity to optimize memory if size is known.
LinkedHashSet<Integer> set = new LinkedHashSet<>(50);
With Initial Capacity + Load Factor
Creates a LinkedHashSet with specified capacity and load factor, controlling resizing behavior.
LinkedHashSet<String> set = new LinkedHashSet<>(32, 0.75f);
From Another Collection
Creates a LinkedHashSet containing all elements from another collection, preserving insertion order.
LinkedHashSet<String> set = new LinkedHashSet<>(list);

add(E e)
Adds a single element to the set; ignores duplicates.
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Java");
set.add("Python");
set.add("Java"); // duplicate ignored
System.out.println(set); // [Java, Python]
addAll(Collection c)
Adds all elements from another collection; duplicates are ignored.
List<String> list = Arrays.asList("C++", "Python", "Go");
set.addAll(list);
System.out.println(set); // [Java, Python, C++, Go]
contains(Object o)
Checks if a specific element exists in the set.
System.out.println(set.contains("Java")); // true
System.out.println(set.contains("Ruby")); // false
isEmpty()
Returns true if the set has no elements.
System.out.println(set.isEmpty()); // false
size()
Returns the total number of elements in the set.
System.out.println(set.size()); // 4
remove(Object o)
Removes a specific element if it exists.
set.remove("Go");
System.out.println(set); // [Java, Python, C++]
clear()
Removes all elements from the set.
set.clear();
System.out.println(set); // []
removeAll(Collection c)
Removes all elements that are present in the given collection.
set.addAll(Arrays.asList("Java", "Python", "C++", "Go"));
set.removeAll(Arrays.asList("Python", "Go"));
System.out.println(set); // [Java, C++]
retainAll(Collection c)
Keeps only the elements that are present in the given collection; removes others.
set.retainAll(Arrays.asList("Java", "Ruby"));
System.out.println(set); // [Java]
Iterator
Iterates through elements in insertion order using an Iterator.
Iterator<String> itr = set.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
For-each Loop
Iterates over elements conveniently while preserving insertion order.
for(String s : set) {
System.out.println(s);
}
| Operation | Time Complexity | Explanation |
|---|---|---|
| add() | O(1) average | Hash-based |
| remove() | O(1) average | Direct bucket removal |
| contains() | O(1) average | Hash lookup |
| iterate() | O(n) | Visit all elements |
Note: Worst-case can degrade to O(n) with poor hashCode() implementation.
| Feature | LinkedHashSet | HashSet |
|---|---|---|
| Order | Maintains insertion order | No order guarantee |
| Underlying Data Structure | Hash table + linked list | Hash table |
| Duplicates | Not allowed | Not allowed |
| Null Elements | Allows one null | Allows one null |
| Performance (Add/Remove/Search) | Slightly slower than HashSet due to linked list overhead | Fastest (O(1) average) |
| Iteration | Predictable order (insertion order) | Unpredictable order |
| Memory Usage | Slightly higher (extra linked list pointers) | Lower (only hash table) |
| When to Use | When insertion order matters | When order does not matter, and performance is critical |
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Java");
set.add("Python");
set.add("C++");
set.add("Java"); // Duplicate ignored
System.out.println(set);
}
}
Output:
[Java, Python, C++]
for(String s : set) {
System.out.println(s);
}
List<Integer> list = Arrays.asList(10, 20, 10, 30, 20);
LinkedHashSet<Integer> unique = new LinkedHashSet<>(list);
System.out.println(unique); // [10, 20, 30]
LinkedHashSet = Unique + Ordered Set