LinkedHashSet in Java implements the Set interface of the Collections Framework.
- It combines the functionalities of a HashSet with a doubly-linked 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.
LinkedHashSetConstructors 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 LinkedHashSet with the elements of the collection C.
LinkedHashSet<E> hs = new LinkedHashSet<E>(Collection c);
3. LinkedHashSet(int initialCapacity)
Used to initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
LinkedHashSet<E> hs = new LinkedHashSet<E>((int initialCapacity);
4. LinkedHashSet(int capacity, float fillRatio)
Creates an empty LinkedHashSet with specified capacity and load factor.
LinkedHashSet<E> hs = new LinkedHashSet<E>(int capacity, float loadFactor);
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 usingthe 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
What is the difference between LinkedHashSet and HashSet?
-
Both store's nonduplicates values
-
Both implements the AbstractSet interface
-
The order of elements is maintained in LinkedHashSet, not in the HashSet
-
Explanation:
HashSet is an unordered & unsorted collection of the data set, whereas the LinkedHashSet is an ordered and sorted collection of HashSet.
Which of the following is true about LinkedHashSet?
-
It allows duplicate elements
-
It maintains insertion order
-
It sorts elements in ascending order
-
Explanation:
LinkedHashSet maintains the insertion order while still using hashing internally.
LinkedHashSet internally uses which data structure?
Explanation:
A LinkedHashSet uses a HashMap with a doubly-linked list to maintain order.
Quiz Completed Successfully
Your Score : 2/3
Accuracy : 0%
Login to View Explanation
1/3 1/3 < Previous Next >
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java
My Profile