coderz.py

Keep Coding Keep Cheering!

LinkedHashSet class in Java Collection Framework

java thread class

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement