Java LinkedHashSet
In this page:
Introduction to LinkedHashSet
LinkedHashSet is an implementation of Set that combines a hash table with an internal doubly-linked list, giving it both the fast lookup speed of a HashSet and a predictable iteration order.
Example: Introduction to LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>(); // hash table + linked list
set.add("a");
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
Insertion Order Preserved
Unlike HashSet, LinkedHashSet always iterates its elements in the exact order they were originally inserted, and re-adding an already-present element does not change its position in that order.
Example: Insertion Order Preserved
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("banana");
set.add("apple");
set.add("banana"); // re-adding doesn't move position
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashSet vs HashSet
LinkedHashSet offers the same average-case performance as HashSet for add, remove, and contains, at the cost of slightly more memory to maintain the linked list, in exchange for a guaranteed, predictable iteration order.
Example: LinkedHashSet vs HashSet
import java.util.LinkedHashSet;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> hash = new HashSet<>();
LinkedHashSet<String> linked = new LinkedHashSet<>();
hash.add("z"); hash.add("a");
linked.add("z"); linked.add("a");
System.out.println(linked); // predictable order
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashSet vs TreeSet
LinkedHashSet differs from TreeSet in what order it maintains: LinkedHashSet preserves the order elements were inserted in, while TreeSet always keeps elements sorted according to their natural ordering or a supplied Comparator.
Example: LinkedHashSet vs TreeSet
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> linked = new LinkedHashSet<>();
TreeSet<String> tree = new TreeSet<>();
linked.add("banana"); linked.add("apple");
tree.add("banana"); tree.add("apple");
System.out.println(linked); // insertion order
System.out.println(tree); // sorted order
}
}
Login to try C/C++/Java/PHP code in the editor
When to Use LinkedHashSet
LinkedHashSet is the right choice whenever duplicates need to be removed but the original order they appeared in still matters, such as tracking a user's unique recent searches or deduplicating a list while preserving its sequence.
Example: When to Use LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> recentSearches = new LinkedHashSet<>();
recentSearches.add("java");
recentSearches.add("java"); // deduplicated
recentSearches.add("python");
System.out.println(recentSearches);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting