LinkedHashSet is a class that is available in the java.util package. It extends the java.util.HashSet class and implements the java.util.Set, java.lang.Cloneable, and java.io.Serializable interfaces.
Important points which we must have to remember:
1. In Java, using LinkedHashSet allows us to store different types of data without duplicity.
2. It stores data in the order they were added.
3. LinkedHashSet uses Node representation to store its elements.
4. Since LinkedHashSet works with nodes, random access is not allowed; elements can be accessed only in sequential order.
5. Internally, LinkedHashSet works as a Doubly Linked List.
6. We can access LinkedHashSet members using an Iterator only; ListIterator and Enumeration methods cannot be used to traverse a LinkedHashSet.
7. As discussed, java.util.LinkedHashSet implements the java.lang.Cloneable and java.io.Serializable interfaces, making a LinkedHashSet eligible for Cloning and Serialization, respectively.
java.util.LinkedHashSet:
public class java.util.LinkedHashSet<E> extends java.util.HashSet<E> implements java.util.Set<E>, java.lang.Cloneable, java.io.Serializable {
public java.util.LinkedHashSet(int, float);
public java.util.LinkedHashSet(int);
public java.util.LinkedHashSet();
public java.util.LinkedHashSet(java.util.Collection<? extends E>);
public java.util.Spliterator
}
Constructor of java.util.LinkedHashSet class:
1. public java.util.LinkedHashSet(): This is the default constructor of java.util.LinkedHashSet class. It helps us create an empty LinkedHashSet with an initial capacity of 16 and a load factor of 0.75.
2. public java.util.LinkedHashSet(java.util.Collection<? extends E>): We use this parameterized constructor to create a LinkedHashSet that will contain all the elements of the specified collection in the same order. The initial capacity will be sufficient to hold all the elements of the collection, and the load factor will be 0.75. Importantly, it removes any duplicate elements from the specified collection.
3. public java.util.LinkedHashSet(int): It creates an empty LinkedHashSet with a specified initial capacity and applies the default load factor, which is 0.75.
4. public java.util.LinkedHashSet(int, float): It constructs an empty LinkedHashSet with a specified initial capacity and load factor.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class JTC {
public static void main(String[] args) {
// Creating Empty LinkedHashSet using default constructor..
LinkedHashSet hashSet1 = new LinkedHashSet();
System.out.println("hasSet1 :- " + hashSet1);
System.out.println("hashSet1 size :- " + hashSet1.size());
System.out.println("hashSet1 is empty :- " + hashSet1.isEmpty());
ArrayList a1 = new ArrayList();
a1.add("Hello");
a1.add(101);
a1.add(true);
a1.add(101);
a1.add(new JTC());
a1.add('A');
System.out.println("\nArray List :- " + a1);
// Creating LinkedHashSet using java.util.LikedHashSet(Collection) constructor..
LinkedHashSet hashSet2 = new LinkedHashSet(a1);
System.out.println("hashSet2 :- " + hashSet2);
System.out.println("hashSet2 size :- " + hashSet2.size());
System.out.println("hashSet2 is empty :- " + hashSet2.isEmpty());
}
}
hasSet1 :- [] hashSet1 size :- 0 hashSet1 is empty :- true Array List :- [Hello, 101, true, 101, com.P1.JTC@156643d4, A] hashSet2 :- [Hello, 101, true, com.P1.JTC@156643d4, A] hashSet2 size :- 5 hashSet2 is empty :- false
In this example, we are working with different possibilities to create a LinkedHashSet. In the first section, we are creating an empty LinkedHashSet using the default constructor of the java.util.LinkedHashSet class.
In the next part, we are creating a LinkedHashSet that contains all the elements of a specified ArrayList. As discussed, the order of the elements in the LinkedHashSet is the same as the ArrayList, and it removes any duplicate elements present in the ArrayList.
Note:- Because java.util.LinkedHashSet is a subclass of java.util.HashSet, we can use the different methods of the java.util.HashSet class along with LinkedHashSet as per our business logic.