LinkedHashSet in Java implements the Set interface of the Collection Framework.
- Combines the functionalities of a HashSet with an internal list to maintain the insertion order of elements.
- LinkedHashSet stores unique elements only and allows a single null.
- Implements Set, Cloneable, and Serializable interfaces.
Java
import java.util.LinkedHashSet;
public class Geeks {
public static void main(String[] args) {
// Create a LinkedHashSet of Strings
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
set.add("Apple");
System.out.println("" + set);
}
}
Output[Apple, Banana, Cherry]
Hierarchy of LinkedHashSet
It implements the Set interface, which is a sub-interface of the Collection interface.
Constructors of LinkedHashSet
1. 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)
Creates an empty LinkedHashSet with specified capacity and load factor.
LinkedHashSet<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 LinkedHashSet
In 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.
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);
}
}
OutputLinkedHashSet : [Geek, For, Geeks]
2. Removing Elements in LinkedHashSet
The values can be removed from the LinkedHashSet using the remove() method.
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"));
}
}
Output[Geek, For, Geeks, A, B, Z]
After removing element [Geek, For, Geeks, A, Z]
false
3. 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.
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();
}
}
OutputGeek, For, Geeks, A, B, Z,
Geek, For, Geeks, A, B, Z,
Methods of LinkedHashSet
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java