← Back to Core Java Course | Chapter 11: Collections | Lesson 13 of 17

Java LinkedHashSet

LinkedHashSet combines a hash table with an internal linked list, giving it the fast lookups of a HashSet along with a predictable, insertion-ordered iteration.

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

java
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);
	}
}

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

java
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);
	}
}

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

java
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
	}
}

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

java
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
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.