Home » Java programming language

Differences between HashSet and LinkedHashSet in Java

HashSet and LinkedHashSet in Java: Here, we are going to learn what are the differences between HashSet and LinkedHashSet in Java programming language?
Submitted by Preeti Jain, on August 05, 2019

HashSet and LinkedHashSet

  • This class is available in java.util package.
  • This is an implementation class of Set interface.
  • HashSet class is the parent of the LinkedHashSet class.
  • The underlying data structure to implement HashSet is Hashtable.
  • In HashSet insertion, the order is not preserved that means the insertion order of the elements is not needed to be the same as the retrieving order of the elements.
  • This HashSet class is introduced in the earlier version of Java 1.2.
  • We should go for HashSet if the insertion order of the elements is not important.

Example:

Let suppose we have a HashSet with few elements. Here we are adding the elements in the order is [10,20,30,50,null] and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like [null.50,20,10,30].

// Java program to demonstrate the behavior of HashSet

import java.util.*;

class HashSetClass {
    public static void main(String[] args) {
        // Creating an instance of HashSet
        HashSet hs = new HashSet();

        // By using add() method to add an elements into the HashSet
        hs.add(10);
        hs.add(20);
        hs.add(30);
        hs.add(50);
        hs.add(null);

        // Display HashSet elements
        System.out.println("Retrieval order of the elements in HashSet is :" + hs);
    }
}

Output

E:\Programs>javac HashSetClass.java

E:\Programs>java HashSetClass
Retrieval order of the elements in HashSet is :[null, 50, 20, 10, 30]

LinkedHashSet

  • This class is available in java.util package.
  • This is an implementation class of Set interface.
  • LinkedHashSet class is the child of the HashSet class.
  • The underlying data structure to implement LinkedHashSet is a combination of Hashtable and LinkedList.
  • In LinkedHashSet insertion order is preserved that means the insertion order of the elements must be the same as the retrieving order of the elements.
  • This LinkedHashSet class is introduced in the earlier version of Java 1.4.
  • We should go for LinkedHashSet if the insertion order of the elements is important.

Example:

Let suppose we have a LinkedHashSet with few elements. Here we are adding the elements in the order is [10,20,30,50,null] and if we are retrieving the elements so the order of retrieving elements must be the same (i.e. it must be the same insertion and retrieval order of the elements.) so the output will be the same and the order will be like [10,20,30,50,null].

// Java program to demonstrate the behavior of LinkedHashSet

import java.util.*;

class LinkedHashSetClass {
    public static void main(String[] args) {
        // Creating an instance of LinkedHashSet
        LinkedHashSet lhs = new LinkedHashSet();

        // By using add() method to add an elements into the LinkedHashSet
        lhs.add(10);
        lhs.add(20);
        lhs.add(30);
        lhs.add(50);
        lhs.add(null);

        // Display LinkedHashSet elements
        System.out.println("Retrieval order of the elements in LinkedHashSet is :" + lhs);
    }
}

Output

E:\Programs>javac LinkedHashSetClass.java

E:\Programs>java LinkedHashSetClass
Retrieval order of the elements in LinkedHashSet is :[10, 20, 30, 50, null]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.