LinkedHashSet in Java
LinkedHashSet in Java is a class that extends HashSet and keeps its elements in insertion order. It is used when you need unique elements, like a HashSet, but also want to get them back in the order they were added. Inside, it uses a hash table for fast lookup plus a doubly linked list that runs through all the elements to remember their order. In this lesson, you will learn how to use LinkedHashSet and how it compares with the other sets.
Declaration
public class LinkedHashSet<E> extends HashSet<E>
implements SequencedSet<E>, Cloneable, SerializableSince Java 21, LinkedHashSet also implements SequencedSet, which adds methods such as getFirst, getLast and reversed.
Features of LinkedHashSet
- Unique elements, like every set.
- Insertion order is kept. Adding an element that is already present does not move it.
- Allows one
null. - Fast:
add,removeandcontainsare constant time on average, a little slower thanHashSetbecause of the links. - Not synchronized.
Example: Order Is Kept
Let's see an example comparing HashSet and LinkedHashSet with the same input.
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class OrderDemo {
public static void main(String[] args) {
List<String> visits = List.of("Pune", "Agra", "Delhi", "Agra",
"Mumbai", "Pune", "Chennai");
Set<String> linked = new LinkedHashSet<>(visits);
System.out.println("LinkedHashSet: " + linked);
Set<String> hash = new HashSet<>(visits);
System.out.println("HashSet size: " + hash.size());
}
}LinkedHashSet: [Pune, Agra, Delhi, Mumbai, Chennai]
HashSet size: 5Explanation: Both sets drop the repeated Agra and Pune and keep 5 cities. The LinkedHashSet lists them in the order they were first visited. A HashSet would list them in an order that depends on hash codes, which is why only its size is printed here.
Example: Sequenced Methods (Java 21)
import java.util.LinkedHashSet;
import java.util.List;
public class SequencedDemo {
public static void main(String[] args) {
LinkedHashSet<String> names = new LinkedHashSet<>(
List.of("Navika", "Srihitha", "Advitha"));
System.out.println("First: " + names.getFirst());
System.out.println("Last: " + names.getLast());
System.out.println("Reversed: " + names.reversed());
names.addFirst("Anamaneni");
System.out.println(names);
}
}First: Navika
Last: Advitha
Reversed: [Advitha, Srihitha, Navika]
[Anamaneni, Navika, Srihitha, Advitha]Explanation: getFirst and getLast read the ends of the order. reversed() returns a reversed view. addFirst puts an element at the front; if it were already in the set, it would be moved there.
HashSet vs LinkedHashSet vs TreeSet
| HashSet | LinkedHashSet | TreeSet | |
|---|---|---|---|
| Order | None | Insertion order | Sorted |
add/contains | O(1) | O(1), slightly slower | O(log n) |
null | One allowed | One allowed | Not allowed |
| Memory | Least | More (links) | More (tree nodes) |
| Use when | Order doesn't matter | Order of arrival matters | Sorted order or ranges matter |
Common Mistakes
Expecting a re-added element to move to the end. add leaves it where it was; remove it first, or use addLast.
Using TreeSet when insertion order is what you need. TreeSet sorts; LinkedHashSet remembers arrival order.
Practice
- Easy: Remove duplicate characters from the word "Anamaneni" while keeping their first-seen order.
- Harder: Keep a "recently viewed" list of product IDs with no duplicates, where viewing a product again moves it to the end.
Key Takeaways
Was this tutorial helpful?