Last Updated : 11 Aug, 2025
Try it on GfG Practice
LinkedHashSet in Java implements the Set interface of the Collection Framework. It combines the functionality of a HashSet with a LinkedList to maintain the insertion order of elements.
Example:
Java
import java.util.LinkedHashSet;
public class Geeks {
public static void main(String[] args) {
// Create a LinkedHashSet of Strings
LinkedHashSet<String> lh = new LinkedHashSet<>();
System.out.println("" + lh);
}
}
Declaring a LinkedHashSet
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
where, E is the type of elements maintained by this set.
LinkedHashSet maintains insertion order using a doubly-linked list. Unlike HashSet, it allows predictable iteration in the order elements were added.
Constructors of LinkedHashSet1. LinkedHashSet(): This constructor is used to create an empty LinkedHashSet with the default capacity i.e. 16 and load factor 0.75.
LinkedHashSet<E> hs = new LinkedHashSet<E>();
2. LinkedHashSet(Collection C): Used in initializing the HashSet with the elements of the collection C.
LinkedHashSet<E> hs = new LinkedHashSet<E>(Collection c);
3. LinkedHashSet(int size): Used to initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
LinkedHashSet<E> hs = new LinkedHashSet<E>(int size);
4. LinkedHashSet(int capacity, float fillRatio): Can be used to initialize both the capacity and the fill ratio, also called the load capacity of the LinkedHashSet with the arguments mentioned in the parameter. When the number of elements exceeds the capacity of the hash set is multiplied with the fill ratio thus expanding the capacity of the LinkedHashSet.
Performing Various Operations on LinkedHashSetLinkedHashSet<E> hs = new LinkedHashSet<E>(int capacity, int fillRatio);
Let’s see how to perform a few frequently used operations on the LinkedHashSet.
1. Adding Elements in LinkedHashSetIn order to add an element to the LinkedHashSet, we can use the add() method. This is different from HashSet because in HashSet, the insertion order is not retained but is retained in the LinkedHashSet.
Example:
Java
import java.io.*;
import java.util.*;
class Geeks {
public static void main(String[] args) {
// Creating an empty LinkedHashSet
LinkedHashSet<String> lh = new LinkedHashSet<String>();
// Adding elements to above Set using add() method
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
System.out.println("LinkedHashSet : " + lh);
}
}
LinkedHashSet : [Geek, For, Geeks]2. Removing Elements in LinkedHashSet
The values can be removed from the LinkedHashSet using the remove() method.
Example:
Java
import java.io.*;
import java.util.*;
class Geeks {
public static void main(String[] args) {
// Creating an empty LinekdhashSet of string type
LinkedHashSet<String> lh
= new LinkedHashSet<String>();
// Adding elements to above Set using add() method
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
lh.add("A");
lh.add("B");
lh.add("Z");
System.out.println("" + lh);
// Removing the element from above Set
lh.remove("B");
// Again removing the element
System.out.println("After removing element " + lh);
// Returning false if the element is not present
System.out.println(lh.remove("AC"));
}
}
[Geek, For, Geeks, A, B, Z] After removing element [Geek, For, Geeks, A, Z] false3. Iterating through the LinkedHashSet
Iterate through the elements of LinkedHashSet using the iterator() method. The most famous one is to use the enhanced for loop.
Example:
Java
import java.io.*;
import java.util.*;
class Geeks {
public static void main(String[] args) {
Set<String> lh = new LinkedHashSet<String>();
lh.add("Geek");
lh.add("For");
lh.add("Geeks");
lh.add("A");
lh.add("B");
lh.add("Z");
Iterator itr = lh.iterator();
while (itr.hasNext())
System.out.print(itr.next() + ", ");
System.out.println();
for (String s : lh)
System.out.print(s + ", ");
System.out.println();
}
}
Geek, For, Geeks, A, B, Z, Geek, For, Geeks, A, B, Z,Advantages of LinkedHashSet
contains(Object o)
Checks if the set contains the specified element. containsAll(Collection c)
Checks if the set contains all elements from the given collection. remove(Object o)
Removes the specified element from the set. removeAll(Collection c)
Removes all matching elements from the set. retainAll(Collection c)
Keeps only elements present in the given collection. isEmpty()
Checks if the set is empty. size() Returns the number of elements in the set. iterator()
Returns an iterator over the elements. toArray() Returns an array containing all elements. toArray(T[] a) Returns an array of the specified type containing all elements. hashCode() Returns hash code of the set. equals(Object o)
Compares this set with another set. clone() Creates a shallow copy of the set. toString() Returns a string representation of the set. spliterator() Returns a Spliterator for this set. stream() Returns a sequential stream of elements. parallelStream() Returns a parallel stream of elements. removeIf(Predicate filter) Removes elements that match a condition. forEach(Consumer action) Performs an action on each element.
Notes:
- LinkedHashSet extends HashSet and maintains insertion order.
- It inherits most methods from HashSet, AbstractSet, AbstractCollection and Set interface.
- Methods like stream(), parallelStream() are useful for working with Java Streams.
- retainAll() and removeAll() are useful for performing set operations (intersection and difference).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4