Java Linkedhashset

The Java LinkedHashSet class is an implementation of the Set interface that combines features of a Hashtable and a linked list. It extends the functionality of the HashSet class while also implementing the Set interface.

The important points about the Java LinkedHashSet class are:

  • Java LinkedHashSet class contains unique elements only like HashSet.
  • Java LinkedHashSet class provides all optional set operations and permits null elements.
  • Java LinkedHashSet class is non-synchronized.
  • Java LinkedHashSet class maintains insertion order.
  • Note: Keeping the insertion order in the LinkedHashset has some additional costs, both in terms of extra memory and extra CPU cycles. Therefore, if it is not required to maintain the insertion order, go for the lighter-weight HashMap or the HashSet instead.

    Hierarchy of LinkedHashSet class

The LinkedHashSet class is an extension of the HashSet class, which is a class that implements the Set interface. The Set interface inherits interfaces such as Collection and Iterable in a hierarchical manner.

LinkedHashSet Class Declaration

Let's examine the declaration of the java.util.LinkedHashSet class.

Example

public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable

Constructors of Java LinkedHashSet Class

Constructor Description
HashSet() It is used to construct a default HashSet.
HashSet(Collection c) It is used to initialize the hash set by using the elements of the collection c.
LinkedHashSet(int capacity) It is used to initialize the capacity of the linked hash set to the given integer value capacity.
LinkedHashSet(int capacity, float fillRatio) It is used to initialize both the capacity and the fill ratio (also called load capacity) of the hash set from its argument.

Java LinkedHashSet Example

An illustration of the Java LinkedHashSet class will demonstrate how elements are iterated in the order of their insertion.

Example

import java.util.*;

class Main{

 public static void main(String args[]){

 //Creating HashSet and adding elements

	    LinkedHashSet<String> set=new LinkedHashSet();

			   set.add("One");  

			   set.add("Two");  

			   set.add("Three"); 

			   set.add("Four");

			   set.add("Five");

			   Iterator<String> i=set.iterator();

			   while(i.hasNext())

			   {

			   System.out.println(i.next());

			   }

 }

}

Output:

Output

One

Two

Three

Four

Five

Note: We can also use the enhanced for loop for displaying the elements.

Java LinkedHashSet example ignoring duplicate Elements

Example

Example

import java.util.*;

class Main{

 public static void main(String args[]){

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

  al.add("Ravi");

  al.add("Vijay");

  al.add("Ravi");

  al.add("Ajay");

  Iterator<String> itr=al.iterator();

  while(itr.hasNext()){

   System.out.println(itr.next());

  }

 }

}

Output:

Output

Ravi

       Vijay

       Ajay

Remove Elements Using LinkeHashSet Class

Example

Example

import java.util.*;



public class Main

{



// main method

public static void main(String argvs[])

{



// Creating an empty LinekdhashSet of string type

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



// Adding elements to the above Set

// by invoking the add() method

lhs.add("Java");

lhs.add("T");

lhs.add("Point");

lhs.add("Good");

lhs.add("Website");



// displaying all the elements on the console

System.out.println("The hash set is: " + lhs);



// Removing an element from the above linked Set



// since the element "Good" is present, therefore, the method remove()

// returns true

System.out.println(lhs.remove("Good"));



// After removing the element

System.out.println("After removing the element, the hash set is: " + lhs);



// since the element "For" is not present, therefore, the method remove()

// returns false

System.out.println(lhs.remove("For"));



}

}

Output:

Output

The hash set is: [Java, T, Point, Good, Website]

true

After removing the element, the hash set is: [Java, T, Point, Website]

false

Java LinkedHashSet Example: Book

Example

Example

import java.util.*;

class Book {

int id;

String name,author,publisher;

int quantity;

public Book(int id, String name, String author, String publisher, int quantity) {

	this.id = id;

	this.name = name;

	this.author = author;

	this.publisher = publisher;

	this.quantity = quantity;

}

}

public class Main {

public static void main(String[] args) {

	LinkedHashSet<Book> hs=new LinkedHashSet<Book>();

	//Creating Books

	Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);

	Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);

	Book b3=new Book(103,"Operating System","Galvin","Wiley",6);

	//Adding Books to hash table

	hs.add(b1);

	hs.add(b2);

	hs.add(b3);

	//Traversing hash table

	for(Book b:hs){

	System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);

	}

}

}

Output:

Output

101 Let us C Yashwant Kanetkar BPB 8

102 Data Communications & Networking Forouzan Mc Graw Hill 4

103 Operating System Galvin Wiley 6

Input Required

This code uses input(). Please provide values below: