Difference and similarities between HashSet , LinkedHashSet and TreeSet in Java


The HashSet, LinkedHashSet and TreeSet are the set interface class mainly used to store the elements.

  • HashSet − A HashSet is a container instance which stores the unique elements only in a non synchronized manner to handle the high-performance operations involving with the set. The set allows the null values which does not follow the order of insertion.

  • LinkedHashSet − The LinkedHashSet is a cloned data structure which have the both facilities of a hashtable and linked list as a set interface. The ordered version of a LinkedHashSet always works as a doubly linked list on over the input elements.

  • TreeSet − A TreeSet is a sorted data structure which works as an interface by using the storage of a tree map. The set follows the general ordering where a comparator class set the value of the latency by using a constructor.

Even though, there are some differences between these interface classes and also have some existing similarities , we have tried to discuss here.

Input

[ ARB, RDD, KOL, DHKA ]

Output

Insertion Order of objects in HashSet : 
[ ARB, RDD, KOL, DHKA ]
Insertion Order of objects in LinkedHashSet : 
[ ARB, RDD, DHKA, KOL ]
Insertion Order of objects in TreeSet :
[ ARB, DHKA, KOL, RDD ]

Similarities Between HashSet, LinkedHashSet and TreeSet

There are so many similarities between HashSet, LinkedHashSet, and TreeSet on the basis of their preferences and modus operandi.

  • Interface of the data set − HashSet, LinkedHashSet, and TreeSet are some common classes which applies as an interface by using inheritance.

  • No duplicacy − These sets do not allow any duplicate contents in it as they are unique in nature.

  • Framework − These are some well known Java collection frames which is able to provide manipulation with an unique way.

Differences Between HashSet, LinkedHashSet and TreeSet

Despite, there are some similarities but, these sets have lots of differences as mentioned below −

Hashset

LinkedHashset

Treeset

HashSet use a hash table to store the data.

Maintains an insertion order by using a doubly linked list.

We always use a self-balanced binary tree as known as Red - Black tree.

Elements sorted into an arbitrary manner.

Works with a predicted iteration order.

Sorted by using some custom comparator.

It works in a constant time frame.

After completion of every step, we can compare the insertion an deletion process.

In a specific range we can retrive the data from first and last with O ( log n ) time frame.

Here, we can say the HashSet is the most efficient and prominent process for the general set actions. On the other hand, the LinkedHashSet always follow the insertion manner where the TreeSet sorts the element itself in an automated structure.

Methods Used

Using the insertion order and insertion time method

Algorithm

The algorithm will show you the systematic process to implement the HashSet, LinkedHashSet and TreeSet structures with some timer values.

  • Step 1 − Start the process.

  • Step 2 − Declare input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a public class.

  • Step 5 − Set the functions.

  • Step 6 − Go for the insertion order.

  • Step 7 − Declare an array list and populate it.

  • Step 8 − Declare the Set values.

  • Step 9 − Print the values by following the insertion manner.

  • Step 10 − Declare a loop to iterate the process.

  • Step 11 − Set the timer value.

  • Step 12 − Run the process ang get the output value.

  • Step 13 − Terminate the process.

Syntax

The syntax will check a treeset first with some integer values. After that we will declare a map set to create some pairs from those elements to filter them up as per the immunity.

TreeSet < Integer > STTREE = new TreeSet < > () ;
STTREE . add ( 4 ) ;
STTREE . add ( 5 ) ;
STTREE . add ( 6 ) ;
STTREE . add ( 8 ) ;
STTREE . add ( 4 ) ;

TreeMap < Integer , Integer > MAPTREE = new TreeMap < > () ;
MAPTREE . put ( 2,5 ) ;
MAPTREE . put ( 3,6 ) ;
MAPTREE . put ( 4,6 ) ;
MAPTREE . put ( 2,3 ) ;

Using the insertion order and insertion time method

An insertion order is an agreement by which an user add some more elements into a data set by using a certain insertion time. The insertion time is an interval span which keep the record of the time consumption by the elements. In this approach, we have used the insertionOrder () function on a linked hashset to perform the process with the O (1) time complexity.

Example

In the example code we have performed the various process like insertion and deletion in a particular time span by using iteration to get the difference the above mentioned maps.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class ARBRDD {
   private static void insertionOrder (){
      LinkedHashSet < String > ARBRDDLINKEDSET
         = new LinkedHashSet <> ();
      TreeSet <String> ARBRDDTREESET = new TreeSet <> ();
      HashSet <String> ARBRDDHASHSET = new HashSet <String> ();
      for ( String str : Arrays.asList ( "ARB", "RDD", "DHKA", "KOL" ) ) {
         ARBRDDLINKEDSET . add ( str ) ;
         ARBRDDTREESET . add ( str ) ;
         ARBRDDHASHSET . add ( str ) ;
      }
      
      System.out.println ( "Insertion Order " + " of objects in HashSet :" + ARBRDDHASHSET ) ;
      System.out.println ( " Insertion Order of " + "objects in LinkedHashSet : " + ARBRDDLINKEDSET ) ;
      System.out.println ( "Insertion Order of" + " objects in TreeSet : " + ARBRDDTREESET ) ;
   }
   private static void insertionTime () {
      HashSet <Integer> numbersHS = new HashSet <> () ;
      long startTime = System . nanoTime () ;
      for  ( int i = 0; i < 1000; i ++ ) {
         numbersHS . add ( i ) ;
      }
      
      long endTime = System . nanoTime () ;
      System.out.println ( "Total time to insert" + " 1000 elements in" + " HashSet in nanoseconds: " + ( endTime - startTime ) ) ;
      LinkedHashSet <Integer> numbersLLS
         = new LinkedHashSet <> () ;

      startTime = System . nanoTime () ;
      for ( int i = 0; i < 1000; i ++ ) {
         numbersLLS . add ( i ) ;
      }
      
      endTime = System . nanoTime () ;
      System.out.println ( "Total time to insert" + " 1000 elements in" + " LinkedHashSet nanoseconds: " + ( endTime - startTime ) ) ;
      TreeSet <Integer> numbersTS = new TreeSet <> () ;

      startTime = System . nanoTime () ;
      for  ( int i = 0; i < 1000; i++ ) {
         numbersTS . add ( i ) ;
      }
      
      endTime = System . nanoTime () ;
      System.out.println ( "Total time to insert" + " 1000 elements in" + " TreeSet in nanoseconds: " + ( endTime - startTime ) ) ;
   }
   private static void deletion () {
      HashSet <Integer> deletionHS = new HashSet <> () ;

      for ( int i = 0; i < 1000; i ++ ) {
         deletionHS . add ( i ) ;
      }

      long startingTime = System . nanoTime () ;
      for ( int i = 0; i < 1000; i ++ ) {
         deletionHS . remove ( i ) ;
      }

      long endedTime = System . nanoTime () ;
      System.out.println("Total time to Deletion " + "1000 elements in HashSet in nanoseconds : " + Math . abs ( startingTime - endedTime ) ) ;
      LinkedHashSet <Integer> deletionLLS
         = new LinkedHashSet <> () ;

      for ( int i = 0; i < 1000; i ++ ) {
         deletionLLS . add ( i ) ;
      }
      startingTime = System . nanoTime () ;
      for ( int i = 0; i < 1000; i ++ ) {
         deletionLLS . remove ( i ) ;
      }

      endedTime = System . nanoTime () ;
      System.out.println (
         "Total time to Deletion 1000"
         + " elements in LinkedHashSet in nanoseconds: "
         + Math . abs ( startingTime - endedTime ) ) ;
         
      TreeSet <Integer> deletionTS = new TreeSet <> () ;
      for ( int i = 0; i < 1000; i ++ ) {
         deletionTS . add ( i ) ;
      }
      
      startingTime = System . nanoTime () ;
      for ( int i = 0; i < 1000; i ++ ) {
         deletionTS . remove ( i ) ;
      }
      
      endedTime = System . nanoTime () ;
      System.out.println(
         "Total time to Deletion 1000"
         + " elements in TreeSet in nanoseconds: "
         + Math . abs ( startingTime - endedTime ) ) ;
   }
   public static void main ( String args [] ){
      insertionOrder () ;
      insertionTime () ;
      deletion () ;
   }
}

Output

Insertion Order  of objects in HashSet : [ ARB, RDD, KOL, DHKA ]
Insertion Order of objects in LinkedHashSet : [ ARB, RDD, DHKA, KOL ]
Insertion Order of objects in TreeSet : [ ARB, DHKA, KOL, RDD ]
Total time to insert 1000 elements in HashSet in nanoseconds : 584880
Total time to insert 1000 elements in LinkedHashSet nanoseconds : 343136
Total time to insert 1000 elements in TreeSet in nanoseconds : 1445318
Total time to Deletion 1000 elements in HashSet in nanoseconds : 339148
Total time to Deletion 1000 elements in LinkedHashSet in nanoseconds : 261723
Total time to Deletion 1000 elements in TreeSet in nanoseconds : 877681

Conclusion

Thus in this article today, we will learned the notable differences between HashSet , LinkedHashSet and TreeSet. As well as there are so many similarities between them as all implement the Set interface. These three structures do not allow the duplicates and not synchronized in manner. We can clone them and resize them by using an iterator as these data sets are fail first in nature.

Updated on: 02-Nov-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements