LinkedHashSet Class in Java

LinkedHashSet Class

LinkedHashSet is also same as the HashSet class. It maintains the insertion order and doesn’t contain duplicate value.

Example : Program to implement the LinkedHashSet class

import java.util.*;
public class LinkedHashSetDemo
{
       public static void main(String args[])
       {
           LinkedHashSet<String> lhs = new LinkedHashSet<String>();
           System.out.println("Initial size of LinkedHashSet: "+lhs.size());
           System.out.println("LinkedHashSet is empty: "+lhs.isEmpty());

           //adding the element
           lhs.add("Mango");
           lhs.add("Orange");
           lhs.add("Grapes");
           lhs.add("Banana");
           lhs.add("Orange");
           lhs.add(null);
           lhs.add(null);

           //retrieving the element
           System.out.println("Elemnts in LinkedHashSet:"+lhs);
           System.out.println("Size of the LinkedHashSet:"+lhs.size());

           //delete the elements
           lhs.remove("Orange");
           System.out.println("Elemnts in LinkedHashSet:"+lhs);
           System.out.println("LinkedHashSet is empty: "+lhs.isEmpty());
       }
}


Output:
Initial size of LinkedHashSet: 0
LinkedHashSet is empty: true
Elemnts in LinkedHashSet:[Mango, Orange, Grapes, Banana, null]
Size of the LinkedHashSet:5
Elemnts in LinkedHashSet:[Mango, Grapes, Banana, null]
LinkedHashSet is empty: false