LinkedHashSet Class

LinkedHashSet Class

In this tutorial, we are going to discuss LinkedHashSet Class in java. It is the child class of HashSet. LinkedHashSet is exactly same as HashSet except the following differences.

LinkedHashSet Class
package com.ashok.collections.hashset;

import java.util.LinkedHashSet;
/**
 * 
 * @author ashok.mariyala
 *
 */
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);
   }
}

Output

[Ashok, Vinod, Dillesh, Thiru, Naresh]
false
[Ashok, Vinod, Dillesh, Thiru, Naresh]
false
[Ashok, Vinod, Dillesh, Thiru, Naresh, null]

Note

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

That’s all about the LinkedHashSet Class in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

LinkedHashSet Class
Scroll to top