Java Programming Handbook

    Java LinkedHashSet

    The LinkedHashSet is a class in the Java Collection Framework that implements the Set interface and extends the HashSet class. It maintains a linked list of the entries in the set, preserving the insertion order of elements. This means that elements will be returned in the order they were inserted.

    It combines hash table performance with linked list ordering, making it a great choice when you need both uniqueness and ordering.

    Characteristics of LinkedHashSet#

    • Maintains insertion order of elements.
    • No duplicate elements allowed.
    • Allows one null element.
    • Non-synchronized.
    • Backed by a hash table with a linked list running through it.

    Constructors of LinkedHashSet#

    ConstructorDescription
    LinkedHashSet()Creates a new, empty set with default capacity and load factor.
    LinkedHashSet(int initialCapacity)Creates a new, empty set with specified initial capacity.
    LinkedHashSet(int initialCapacity, float loadFactor)Creates a new, empty set with specified initial capacity and load factor.
    LinkedHashSet(Collection<? extends E> c)Creates a set containing the elements of the specified collection.

    Example:#

    Set<String> set1 = new LinkedHashSet<>(); Set<Integer> set2 = new LinkedHashSet<>(50); Set<Double> set3 = new LinkedHashSet<>(20, 0.75f); List<String> list = Arrays.asList("A", "B", "C"); Set<String> set4 = new LinkedHashSet<>(list);

    Commonly Used Methods#

    LinkedHashSet inherits all methods from the Set and Collection interfaces:

    MethodDescription
    add(E e)Adds the specified element if not already present.
    remove(Object o)Removes the specified element if present.
    contains(Object o)Returns true if the set contains the element.
    size()Returns the number of elements in the set.
    isEmpty()Returns true if the set is empty.
    clear()Removes all elements from the set.
    iterator()Returns an iterator over the elements in insertion order.

    Example:#

    import java.util.*; public class LinkedHashSetMethodsDemo { public static void main(String[] args) { Set<String> fruits = new LinkedHashSet<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Mango"); fruits.add("Banana"); // duplicate, will be ignored System.out.println("Fruits: " + fruits); System.out.println("Contains Apple? " + fruits.contains("Apple")); System.out.println("Size: " + fruits.size()); fruits.remove("Banana"); System.out.println("After removing Banana: " + fruits); fruits.clear(); System.out.println("After clear(): " + fruits); } }

    Output:#

    Fruits: [Apple, Banana, Mango] Contains Apple? true Size: 3 After removing Banana: [Apple, Mango] After clear(): []

    Performance Analysis#

    OperationTime Complexity
    add(E e)O(1) on average
    remove(Object o)O(1) on average
    contains(Object o)O(1) on average
    iterationO(n)
    • Faster than TreeSet, but slightly slower than HashSet due to maintaining order.
    • Ideal when both fast access and insertion order are needed.

    Use Cases#

    • When you need to maintain insertion order.
    • Removing duplicates from a list while keeping original order.
    • Caching or history tracking systems.

    Example: Removing Duplicates from List While Maintaining Order#

    import java.util.*; public class UniqueOrderedList { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Alice", "David", "Bob"); Set<String> uniqueNames = new LinkedHashSet<>(names); System.out.println("Unique Names: " + uniqueNames); } }

    Output:#

    Unique Names: [Alice, Bob, David]

    Conclusion#

    In this blog, we explored the LinkedHashSet class in Java. It offers the best of both worlds: the uniqueness property of a Set and the ordering of a List. We covered its characteristics, constructors, key methods, performance aspects, and use cases with examples. It's an excellent choice when you need a unique set with predictable iteration order.

    Last updated on Apr 09, 2025