Categories: Java

LinkedHashSet class in Java Collection Framework

The LinkedHashSet class is an ordered HashSet with a doubly-linked List across all entries. This class is used when the iteration order must be maintained. The order of the elements in a HashSet is unpredictable, whereas a LinkedHashSet allows us to iterate through them in the order in which they were inserted.

Declaration of LinkedHashSet:
public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
The following are the key features of the Java LinkedHashSet :
  • The Java LinkedHashSet class, like HashSet, only has unique elements.
  • The Java LinkedHashSet class supports all optional set operations and allows for null items.
  • The LinkedHashSet class in Java is not synchronized.
  • The LinkedHashSet class in Java maintains insertion order.
LinkedHashSet Constructors:

1) LinkedHashSet(): This constructor is used to create a default HashSet.

LinkedHashSet<E> hobj = new LinkedHashSet<E>();

2) LinkedHashSet(Collection C): Used in initializing the HashSet with the elements of the collection C.

LinkedHashSet<E> hobj = new LinkedHashSet<E>(Collection c);

3) LinkedHashSet(int size): The integer specified in the argument is used to initialize the size of the LinkedHashSet.

LinkedHashSet<E> hobj = new LinkedHashSet<E>(int size);

4) LinkedHashSet(int capacity, float fillRatio): It is used to set the capacity and fill ratio (also known as load capacity) of the hash set from its input.

LinkedHashSet<E> hobj = new LinkedHashSet<E>(int capacity, int fillRatio);
LinkedHashSet Method:

It Inherits methods from the HashSet class. Therefore, we can apply all the HashSet operations with LinkedHashSet.

Example: Add & Remove Elements from LinkedHashSet

import java.io.*;
import java.util.*;

class Coderz {

 // Main driver method
 public static void main(String[] args)
 {

  // Creating an empty LinekdhashSet of string type
  LinkedHashSet<String> hobj
   = new LinkedHashSet<String>();

  // Adding elements to above Set
  // using add() method
  hobj.add("Coderz ");
  hobj.add("For");
  hobj.add("Coding");
  hobj.add("JAVA");
  hobj.add("C");
  hobj.add("Python");

  // Printing all above elements to the console
  System.out.println("Initial HashSet " + hobj);

  // Removing the element from above Set
  hobj.remove("B");

  // Again removing the element
  System.out.println("After removing element " + hobj);

  // Returning false if the element is not present
  System.out.println(hobj.remove("AC"));
 }
}
Output:
Initial HashSet [Coderz , For, Coding, JAVA, C, Python]
After removing element [Coderz , For, Coding, JAVA, C, Python]
false
Example: Get the size of LinkedHashSet

import java.io.*;
import java.util.*;

class Coderz {

 // Main driver method
 public static void main(String[] args)
 {

  // Creating an empty LinekdhashSet of string type
  LinkedHashSet<String> hobj
   = new LinkedHashSet<String>();

  // Adding elements to above Set
  // using add() method
  hobj.add("Coderz ");
  hobj.add("For");
  hobj.add("Coding");
  hobj.add("JAVA");
  hobj.add("C");
  hobj.add("Python");

  // Printing all above elements to the console
  System.out.println("Initial HashSet " + hobj);

 System.out.println("Total elements : "+hobj.size());
  
 }
}
Output:
Initial HashSet [Coderz , For, Coding, JAVA, C, Python]
Total elements : 6

Note: also read about the HashSet class in Java Collection Framework

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

21 hours ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago