Difference Between Hashtable and Synchronized Map in Java


A HashTable is a compact abstract data set which converts the keys of a map to the values by computing the indexes into an array of slots to enable faster data access. On the other hand, a synchronized map is a Java collection class which is mainly used to synchronize a particular map to make it a thread safe collection and can be applied on a whole object. The map does not contain any null value.

Input

[ ARB, RDD, KOL, DHKA ]

Output

Insertion Order of objects in HashTable : 
[ ARB, RDD, KOL, DHKA ]
Insertion Order of objects in Synchronized Map :
[ ARB, RDD, DHKA, KOL ]

Methods Used

Using the Synchronization at the Object level

Algorithm

In this algorithm we have declared some functions to perform the insertion order in an array list by considering a set value. Further, by performing an iteration with a loop we will set a time for the length traversal.

  • 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 and 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 of identity from those elements to filter them up as per their state by using the hasNext () .

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

IdentityHashMap < Integer, String > ihmap
   = new IdentityHashMap < Integer, String > () ;
ihmap . put ( 10, "ARB" ) ;
ihmap . put ( 20, "RDD" ) ;
ihmap . put ( 30, "ARBRDD" ) ;
ihmap . put ( 40, "KOLDHKA" ) ;
ihmap . put ( 50, "You" ) ;

PRINT THE VALUE HERE ( " IdentityHashMap size : " + ihmap . size () ) ;
PRINT THE VALUE HERE ( " Initial identity hash map :  "     + ihmap ) ;

Hashtable < Integer, String > table  
   = new Hashtable < Integer, String > () ;  
table . put ( 1 , "X" ) ;
table . put ( 2 , "Y" ) ;
table . put ( 3,  "Z" ) ; 
table . put ( 4,  "A" ) ;

for ( Map . Entry m : table . entrySet () )
   Iterator < IdentityHashMap . Entry < Integer, String > >
   itr = ihmap . entrySet () . iterator () ;
while ( itr . hasNext () ) {
   IdentityHashMap . Entry < Integer, String > entry
      = itr . next ( ) ;
   TreeMap < Integer,Integer > MAPTREE 
      = new TreeMap <> () ;
   MAPTREE . put ( 2,5 ) ;
   MAPTREE . put ( 3,6 ) ;
   MAPTREE . put ( 4,6 ) ;
   MAPTREE . put ( 2,3 ) ;
}

Differences Between Hashtable and Synchronized Map

HashTable

Synchronized Map

HashTable use a table to store the data.

This structure empower a thread to maintain its safety with concurrent access.

Elements sorted into an arbitrary manner.

It follows the manual synchronization with low errors.

It works in a constant time frame.

An API friendly process with the performance overhead.

The HashTable is a synchronized process to implement the map interface by providing the thread safety, which comes with a potential cost of the process. On the other side, SynchronizedMap wrap down the present map class with an inbuilt structure of inheritance to ensure the safety of the running thread.

Using the Synchronization at the Object Level Method

In this approach we have used an object level synchronization process by using the set interface. An object level synchronization is a process when an user wants to execute a specific block on a provided instance of the class.

Example

In the example 1 ( A ), we have input some data elements as integers and strings to synchronize them up into a map by using the getKey () and getValue () functions.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.*;
public class ARBRDD{
   public static void main ( String args [] ){
      HashMap < Integer, String > hmap = new HashMap < Integer, String > () ;
      hmap . put ( 2, "A" ) ;
      hmap . put ( 44, "R" ) ;
      hmap . put ( 1, "B" ) ;
      hmap . put ( 4, "RDD" ) ;
      hmap . put ( 88, "XYZ" ) ;
      
      Map map = Collections . synchronizedMap ( hmap ) ;
      Set set = map . entrySet () ;
      synchronized ( map ){
         Iterator i = set . iterator () ;
         while ( i . hasNext () ){
            Map . Entry me = ( Map .Entry ) i . next () ;
            System.out.print ( me . getKey () + ": " ) ;
            System.out.println ( me . getValue () ) ;
         }
      }
   }
}

Output

1: B
2: A
4: RDD
88: XYZ
44: R

Conclusion

Thus, in this article we have tried to dustinguish the possible differences between a HashTable and a Sunchronized Map with different parametes. To maintain the high level data consistency we only can use the synchronized maps. Though hastable is not recommended for this situation as it is a legacy class.

Updated on: 02-Nov-2023

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements