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

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

28 minutes ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

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

1 month ago

Estimate π Using Monte Carlo Method

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

2 months ago