LinkedHashSet
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.
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.
LinkedHashSet Class