Java.What is LinkedHashSet ?

LinkedHashSet<E> is a subclass of HashSet that maintains the insertion order of elements. Unlike HashSet, which does not guarantee any order, LinkedHashSet preserves the order in which elements were inserted.


1. Key Features of LinkedHashSet

No duplicate elements (like any Set).
Maintains insertion order (Unlike HashSet, which is unordered).
Uses a HashTable + Doubly Linked List internally to store elements.
O(1) average time complexity for add, remove, and contains operations (like HashSet).


2. Internal Working of LinkedHashSet

  • LinkedHashSet extends HashSet and internally uses a LinkedHashMap.
  • It maintains a linked list of elements in insertion order.
  • This makes it slightly slower than HashSet (due to extra memory for maintaining order).

3. Example Usage of LinkedHashSet

Example 1: Basic Usage

import java.util.LinkedHashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> names = new LinkedHashSet<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add("Bob");  // Duplicate, won't be added

        System.out.println(names);  // Output: [Alice, Bob, Charlie] (Insertion Order Maintained)
    }
}
  • Duplicates are ignored (Bob is not added twice).
  • Insertion order is preserved (Alice → Bob → Charlie).

Example 2: Difference Between HashSet and LinkedHashSet

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<String> hashSet = new HashSet<>();
        hashSet.add("Banana");
        hashSet.add("Apple");
        hashSet.add("Mango");
        System.out.println("HashSet: " + hashSet); 
        // Output: [Apple, Mango, Banana] (Unordered)

        Set<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Banana");
        linkedHashSet.add("Apple");
        linkedHashSet.add("Mango");
        System.out.println("LinkedHashSet: " + linkedHashSet); 
        // Output: [Banana, Apple, Mango] (Maintains Insertion Order)
    }
}
  • HashSet does not maintain order.
  • LinkedHashSet keeps elements in insertion order.

4. Performance Comparison

FeatureHashSetLinkedHashSetTreeSet
OrderingUnorderedInsertion OrderSorted Order
Duplicates❌ No❌ No❌ No
Performance (Add/Delete/Contains)O(1)O(1) (Slightly slower due to linked list)O(log n)
UsesHash TableHash Table + Linked ListRed-Black Tree
Memory UsageLowerHigher (due to Linked List)Higher

5. When to Use LinkedHashSet?

Use LinkedHashSet if:

  • You need to maintain insertion order.
  • You need fast lookup (O(1)) but also want ordered elements.
  • You don’t need sorted order (use TreeSet for that).

Avoid LinkedHashSet if:

  • Order doesn’t matter (Use HashSet for better performance).
  • You need elements sorted (Use TreeSet instead).

Final Thoughts

  • LinkedHashSet is like HashSet but preserves insertion order.
  • It uses a Linked List + Hash Table internally.
  • Faster than TreeSet but slightly slower than HashSet.
  • Ideal when you need unique elements in the order they were added.
This entry was posted in Без рубрики. Bookmark the permalink.