LinkedHashSet retains insertion order, while TreeSet stores comparator-ordered unique elements and uses that comparison to decide whether two entries belong to the same set.
Java LinkedHashSet and TreeSet: order and uniqueness
Java 8+. The program uses only JDK classes and runs without a framework.
Pick the meaning of duplicate
A dispatch screen receives warehouse codes in arrival order and must display each code once. LinkedHashSet expresses both requirements without sorting the list or maintaining a second seen-code collection. Adding the same code again does not move its first occurrence in this Java 8 example.
A TreeSet answers a different question: which unique entries fall in sorted order or within a requested range? The sample uses a case-insensitive comparator to show that comparison can collapse values that String.equals would consider different. "Dock-A" and "dock-a" occupy one comparator-equivalence position.
Decide whether that collapse is intended. A shipment comparator that compares only priority can discard distinct shipment IDs with the same priority. Add a stable tie-breaker when both entries must survive. A set is not a sorted multiset, and it does not promise to retain every object passed to add.
Mutable comparison fields break the search model
If an entry changes its ordering field while inside a tree, its physical location can disagree with its new comparison value. A later contains or remove may fail to find it. Store immutable keys, or remove the entry before editing and then insert the changed value.
LinkedHashSet still relies on equals and hashCode. Changing a hashed key has similar lookup problems. Sorting after every display refresh is not automatically cheaper or safer than choosing a collection whose contract matches the screen. Compare HashSet and TreeMap when key/value lookup is also required.
Working program
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class DockOrder {
public static void main(String[] args) {
LinkedHashSet<String> arrival = new LinkedHashSet<>(
Arrays.asList("Dock-C", "Dock-A", "Dock-C", "Dock-B"));
System.out.println(arrival);
TreeSet<String> normalized = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
normalized.add("Dock-A");
normalized.add("dock-a");
normalized.add("Dock-C");
System.out.println("unique=" + normalized.size());
System.out.println("next=" + normalized.ceiling("Dock-B"));
}
}Output
[Dock-C, Dock-A, Dock-B]
unique=2
next=Dock-CCost and failure boundaries
LinkedHashSet offers expected constant-time basic hashed operations when hashes distribute suitably, with extra links for traversal order. TreeSet basic lookup, insertion and deletion have O(log n) comparison counts. Expensive comparisons multiply that cost, so comparing large text keys is not the same work as comparing small integers.
Both retain O(n) entries plus implementation overhead. No exact bytes-per-entry number is portable across JVM configurations. Range views remain attached to their backing sorted collection, and neither class makes compound concurrent updates safe. Review view lifetime before returning a subset from a service.
Common Mistakes
- Do not compare only priority when equal-priority records must coexist.
- Do not mutate ordering or hashed key fields while stored.
- Do not expect sorted order from an insertion-ordered set.
Continue with ownership and failure checks
Continue with Java TreeSet comparator collisions: compare zero means one slot.
