CKTIS / Java
util.LinkedHashSet: practical reference
util.LinkedHashSet: practical reference: LinkedHashSet<E> combines HashSet with a doubly linked list, providing uniqueness and an encounter order normally matching insertion order.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.util.LinkedHashSetLinkedHashSet combines HashSet with a doubly linked list, providing uniqueness and an encounter order normally matching insertion order.
Use it to deduplicate while preserving input order, or when a result must be both a Set and predictably iterable.
It is not synchronized; ordinary add of an existing element does not change order, and mutating an element hash can still break lookup.
In Java 21 it implements SequencedSet with addFirst, addLast, and reversed; iteration costs size regardless of capacity, while fail-fast remains best effort.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var set = new java.util.LinkedHashSet<String>();
set.add("b");
set.add("a");
set.add("b");
System.out.println(set);
}
}
Result
[b, a]
The example exercises the documented element in a small deterministic program and compares exact standard output.