LinkedHashSet Class

LinkedHashSet

  • It is the child class of HashSet.
  • LinkedHashSet is exactly same as HashSet except the following differences.
package com.ashok.collections.hashset;

import java.util.LinkedHashSet;

public class MyLinkedHashSet {
   public static void main(String args[]) {
      // HashSet declaration
      LinkedHashSet<string> hset = new LinkedHashSet<string>();
      // Adding elements to the HashSet
      hset.add("Ashok");
      hset.add("Vinod");
      hset.add("Dillesh");
      hset.add("Thiru");
      hset.add("Naresh");
      System.out.println(hset);
  
      // Addition of duplicate elements
      System.out.println(hset.add("Ashok"));

      System.out.println(hset);
  
      // Addition of null values
      hset.add(null);
      System.out.println(hset.add(null));
  
      // Displaying HashSet elements
      System.out.println(hset);
   }
}

Note

LinkedHashSet and LinkedHashMap commonly used for implementing “cache applications” where insertion order must be preserved and duplicates are not allowed.

LinkedHashSet Class
Scroll to top