Difference between hashset and linkedhashset – LinkedHashSet in Java with Example | Java LinkedHashSet class Declaration, Constructors, Methods & Sample Programs

Difference between hashset and linkedhashset: LinkedHashSet in Java is a class that is present in java.util package. It is the child class of HashSet and implements the set interface. Earlier we have discussed both HashSet and TreeSet in a detailed way and in this tutorial, we are going to share knowledge on LinkedHashSet. It is the same as HashSet and TreeSet but there are slight changes you may observe. You guys can get complete details

Java LinkedHashSet Class Declaration

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

Type Parameters:

E – the type of elements maintained by this set

Implemented Interfaces:

  • Serializable
  • Cloneable,
  • Iterable<E>
  • Collection<E>
  • Set<E>

Hierarchy of LinkedHashSet class

LinkedHashSet in Java with Example

Do Check:

Features of LinkedHashSet Class

  1. LinkedHashSet class contains unique elements.
  2. It allows null value.
  3. The underlying data structure for LinkedHashSet is both a hash table and a linked list.
  4. In LinkedHashSet class, the insertion order is preserved.

Difference between HashSet and LinkedHashSet

Linkedhashset in java: LinkedHashSet is similar to HashSet except for the below-mentioned differences.

1. In HashSet insertion order is not preserved it means the elements will not be returned in the same order in which we are inserted, while in the case of LinkedHashSet insertion order must be preserved.

2. The underlying data structure for HashSet is a hash table while the underlying data structure for LinkedHashSet is both hash table and Linked List.

3. HashSet class came in Java 1.2 version while LinkedHashSet class came in Java 1.4 version.

Constructors of Java LinkedHashSet Class

There are 4 constructors are available in the Java HashSet class, which are described below:

1. LinkedHashSet(): This constructor creates a default linked hash set.

2. LinkedHashSet(int capacity): It is used to initialize the capacity of the linked hash set to the given capacity.

3. LinkedHashSet(int capacity, float loadFactor): This constructor is used to initialize the capacity of the linked hash set to the given capacity and the given load factor.

4. LinkedHashSet(Collection<? extends E> c): It is used to initializes the linked hash set by using the elements of collection c.

Creating LinkedHashSet from Other Collections

From the below sample program, you guys can easily learn how we can create a linked hash set containing all the elements of other java collections:

LinkedHashSet creation program

Output:

ArrayList: [2, 4]
LinkedHashSet: [2, 4]

Methods of LinkedHashSet Class in Java

LinkedHashSet class in Java is the child class of HashSet class. It defines the same methods which are available in the HashSet class and it doesn’t add methods of its own.

Method Description
clone() Creates a copy of the LinkedHashSet
size() Returns the size of the LinkedHashSet
isEmpty() Checks if the LinkedHashSet is empty
contains() Searches the LinkedHashSet for the specified element and returns a boolean result
clear() Removes all the elements from the LinkedHashSet

Insert Elements to LinkedHashSet Class Example

import java.util.LinkedHashSet;

class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> evenNumber = new LinkedHashSet<>();

// Using add() method
evenNumber.add(2);
evenNumber.add(4);
evenNumber.add(6);
System.out.println("LinkedHashSet: " + evenNumber);

LinkedHashSet<Integer> numbers = new LinkedHashSet<>();

// Using addAll() method
numbers.addAll(evenNumber);
numbers.add(5);
System.out.println("New LinkedHashSet: " + numbers);
}
}

Output:

LinkedHashSet: [2, 4, 6]
New LinkedHashSet: [2, 4, 6, 5]

Java LinkedHashSet Example

import java.util.*;

class linkedHashSetExample

{

public static void main(String args[])

{

//creating a hash set

LinkedHashSet lhset = new LinkedHashSet();

//adding elements in linked hash set

lhset.add("Amit");

lhset.add("Sumit");

lhset.add("Rohit");

lhset.add("Virat");

lhset.add("Vijay");

lhset.add("Ajay");

//adding duplicate values

lhset.add("Rohit");

lhset.add("Sumit");

//find size of linked hash set

System.out.println("Size of the linked hash set is: "+lhset.size());

//displaying original linked hash set

System.out.println("The original linked hash set is: " +lhset);

//removing element from linked hash set

System.out.println("Removing(Rohit) from the linked hash set: " +lhset.remove("Rohit"));

//checking specified element present or not

System.out.println("(Sumit)present in the linked hash set or not: " +lhset.contains("Sumit"));

//displaying updated linked hash set

System.out.println("Finally, the updated linked hash set is: "+lhset);

}

}

Output:

Size of the linked hash set is: 6
The original linked hash set is: [Amit, Sumit, Rohit, Virat, Vijay, Ajay]
Removing(Rohit) from the linked hash set: true
(Sumit) present in the linked hash set or not: true
Finally, the updated linked hash set is: [Amit, Sumit, Virat, Vijay, Ajay]