Java


Java Tutorial


Admission Enquiry Form

  

LinkedHashSet Class in Java



LinkedHashSet Class

LinkedHashSet class is a Hashtable and Linked list implementation of the Set interface. It inherits the HashSet class and implements the Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order.
However, LinkedHashSet maintain a doubly-linked list internally for all of its elements.

Java LinkedHashSet class contains unique elements only like HashSet.

Java LinkedHashSet class maintains insertion order.

Syntax for LinkedHashSet class as:

class LinkedHashSet <T>

Note: Here, <T> represents the generic type parameter. It represents which type of elements are being stored into the LinkedHashSet. Suppose, we want to create a LinkedHashSet to store a group of Strings, then we can create the object as:

LinkedHashSet<String> lhs=new LinkedHashSet<String>();






HashSet Class constructors are as:

  • LinkedHashSet();

  • This constructor is used to create a default HashSet.
    e.g. LinkedHashSet hs = new LinkedHashSet();
  • LinkedHashSet(Collection C);

  • This constructor initializing the HashSet with the elements of the collection C.
    e.g. LinkedHashSet hs = new LinkedHashSet(Collection c);
  • HashSet(int capacity);

  • Here, capacity is used initialize the size of the LinkedHashSet with the integer mentioned in the parameter.
    e.g.LinkedHashSet hs = new LinkedHashSet(int capacity);
  • LinkedHashSet(int capacity,float loadfactor);

  • This constructor can be used to initialize both the capacity and the loadfactor, also called the fillratio of the LinkedHashSet with the arguments mentioned in the parameter. When the number of elements exceeds the capacity of the hash set is multiplied with the load factor thus expanding the capacity of the LinkedHashSet.





HashSet Class Methods:

Following are the methods of Java HashSet class are as follows:

S.No. Modifier & Type Method Description
1) boolean add(obj) This method adds an element obj to the HashSet. It returns true if the element is added to the HashSet, else it returns false.
2) boolean remove(obj) It is used to remove all of the elements from the HashSet, if it is present. It returns true if the element is removed successfully.
3) void clear() It is used to remove all of the elements from the HashSet.
4) boolean contains(obj) It is used to return true if the HashSet contains the specified element obj.
5) boolean isEmpty() It is used to return true if this set contains no elements.
6) int size() This returns the number of elements present in the HashSet.





Java LinkedHashSet Example

// Java Program to Illustrate LinkedHashSet // Importing required classes import java.util.LinkedHashSet; // Main class // LinkedHashSetExample public class HashSetExample { // Main driver method public static void main(String[] args) { // Creating an empty LinkedHashSet of string type LinkedHashSet linkedset = new LinkedHashSet(); // Adding element to LinkedHashSet // using add() method linkedset.add("C Programming"); linkedset.add("C++ Programming"); linkedset.add("Java Programming"); linkedset.add("HTML"); // Note: This will not add new element // as HTML already exists linkedset.add("HTML"); linkedset.add("CSS"); // Getting size of LinkedHashSet // using size() method System.out.println("Size of LinkedHashSet = "+linkedset.size()); System.out.println("Original LinkedHashSet:"+linkedset); // Removing existing entry from above Set // using remove() method System.out.println("Removing HTML from LinkedHashSet: "+ linkedset.remove("HTML")); // Removing existing entry from above Set // that does not exist in Set System.out.println( "Trying to Remove Java Script which is not "+"present: " + linkedset.remove("Java Script")); // Checking for element whether it is present inside // Set or not using contains() method System.out.println("Checking if Java is present="+ linkedset.contains("Java")); // Now lastly printing the updated LinkedHashMap System.out.println("Updated LinkedHashSet: "+ linkedset); }//end of main }//end of class


Output:

Size of LinkedHashSet = 5
Original LinkedHashSet:[C Programming, C++ Programming, Java Programming, HTML, CSS]
Removing HTML from LinkedHashSet: true
Trying to Remove Java Script which is not present: false
Checking if Java is present=false
Updated LinkedHashSet: [C Programming, C++ Programming, Java Programming, CSS]