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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

6 days ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

2 weeks ago

Find Intersection of Two Singly Linked Lists

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

6 months ago

Minimum Cost to Paint Houses with K Colors

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

6 months ago

Longest Absolute Path in File System Representation

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

7 months ago

Efficient Order Log Storage

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

7 months ago