Last Updated: 30 September, 2023
The LinkedHashSet class of the Java collections framework was introduced in the JDK 1.4 version and is available in the java.util package.
LinkedHashSet is the Hashtable and linked list implementation of the Set interface with preserved iteration order. The linked list defines the iteration order, which is the order in which elements are inserted into the set. Insertion order is not affected if an element is re-inserted into the set.
LinkedHashSet is a child class of HashSet, so it has all the functionalities of the HashSet class. The HashSet does not maintain the insertion order of elements, whereas the LinkedHashSet maintains the element insertion order. This one is the main difference between both.
As we can see from the complete hierarchy above, LinkedHashSet extends the HashSet class and implements the Set interface as well as Serializable and Cloneable interfaces.
Here, E defines the type of elements that the Set will contain.
Output
LinkedHashSet elements: [800, 500, null, 200]
The Java LinkedHashSet class has the following constructors:
1. public LinkedHashSet() This construct is used to create a new and empty LinkedHashSet object with a default initial capacity is 16 and a load factor is 0.75 LinkedHashSet<E> set = new LinkedHashSet<E>();
|
2. public LinkedHashSet(int initialCapacity) This constructor is used to create a new and empty LinkedHashSet object in which the initial capacity is specified in the construct parameter and default load factor (0.75) LinkedHashSet<E> set = new LinkedHashSet<E>(int initialCapacity);
It will through IllegalArgumentException if the initial capacity is less than zero. |
3. public LinkedHashSet(int initialCapacity, float loadFactor) This constructor is used to create a new and empty LinkedHashSet object in which the initialCapacity and loadFactor are specified in the construct parameters. LinkedHashSet<E> set = new LinkedHashSet<E>(int initialCapacity, float loadFactor);
Note: It will throw a IllegalArgumentException if the initial capacity value is less than zero, or if the load factor is non-positive value. |
4. public LinkedHashSet(Collection<? extends E> c) This constructor is used to create a LinkedHashSet object that contains the elements in the specified collection with a default load factor (0.75) and an initial capacity sufficient to contain the elements in the specified collection. LinkedHashSet<E> set = new LinkedHashSet<E>(Collection C);
Note: It will throw a NullPointerException if the specified collection is null. |
The Java LinkedHashSet class provided methods are implemented in the below given examples. Let's go through the examples one by one and understand the uses for each method.
Example 1 : Methods for Adding elements in LinkedHashSet
Output
LinkedHashSet elements: [800, 500, null, 200]
LinkedHashSet elements after adding another LinkedHashSet
[800, 500, null, 200, 10, 20, 30, 40]
Example 2 : Methods for Accessing elements from LinkedHashSet
Output
LinkedHashSet elements: [10, 20, 30, 40, 50]
Access LinkedHashSet using Iterator
10
20
30
40
50
Access LinkedHashSet using for loop
10
20
30
40
50
Example 3 : Methods for Removing elements from LinkedHashSet
Output
LinkedHashSet elements: [10, 20, 30, 40, 50, 60, 70]
After calling remove() method: [10, 20, 30, 50, 60, 70]
After calling removeAll() method: [10, 30, 50]
After calling removeIf() method: [30, 50]
After calling retainingAll() method: []
HashSet before calling clear() method: [20, 60, 70]
After calling clear() method: []
Example 4 : Utility methods of LinkedHashSet
Output
LinkedHashSet elements: [50, 30, 70, 10, 40, 20, 80]
LinkedHashSet Size: 7
Is LinkedHashSet Empty: false
LinkedHashSet contains() method: true
LinkedHashSet containsAll() method: false
ArrayList elements: [50, 30, 70, 10, 40, 20, 80]
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/LinkedHashSet.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why did Java introduce the LinkedHashSet class when the HashSet and TreeSet classes were already there?
Ans. All three classes implemented a Set Interface where no duplicate value is allowed, and this one is a common feature for all three classes. But the way of storing the value in the set is different.
HashSet stores values in an unordered way, whereas TreeSet stores values in ascending order. That's why Java introduced a new class named LinkedHashSet in the JDK 1.4 version. LinkedHashSet stores the elements in an ordered way.
In Java, what is the difference between HashSet and LinkedHashSet?
Ans. HashSet and LinkedHashSet are both implementations of the Set interface, and LinkedHashSet is a child class of the HashSet class. In the below-given table, we will see the differences between HashSet and LinkedHashSet:
HashSet | LinkedHashSet |
---|---|
The underlying data structure is a hash table. | The underlying data structure is a hash table + linked list (doubly linked list). |
Insertion order is not preserved. | Insertion order is preserved. |
HashSet requires less memory than LinkedHashSet. | LinkedHashSet requires more memory than HashSet. |
HashSet is a parent class of LinkedHashSet. | LinkedHashSet is a child class of HashSet. |
Introduced in the JDK 1.2 version. | Introduced in the JDK 1.4 version. |
What is the difference between LinkedHashSet and TreeSet in Java?
Ans. LinkedHashSet and TreeSet both implement the Set interface. Both have many differences. Let's understand with the help of the given table.
LinkedHashSet | TreeSet |
---|---|
The underlying data structure is a hash table + linked list (doubly linked list). | The underlying data structure of the TreeSet is the Balanced Tree. |
Preserve the iteration order (ordered set). | Store the elements in a sorted order (default, in ascending order). |
Introduced in the JDK 1.4 version. | Introduced in the JDK 1.2 version. |
Allows us to insert a null value. | Does not allow us to insert a null. |