Difference between Hashtable and HashMap in Java

Here you will learn about difference between Hashtable and HashMap in Java.

Both Hashtable and HashMap are data-structure based upon hashing and implementation of Map interface, and almost seems to be similar but there is significant difference between both which is described as follows.

Characteristic Hashtable HashMap
Synchronization Hashtable is  synchronized internally.

Synchronization means that merely one thread can change that Hashtable at one time. Fundamentally it signify that  Hashtable will have to attain a lock on the object prior to programming an update on Hashtable while others will stop for the lock to be released. We can  more evidently say that  at a given time more than one thread does not have right to use the Hashtable which is ensured by synchronization. Hashtable can be shared between multiple threaded as it is thread safe.

HashMap is non-synchronized.

This means that  more than one thread can access and process the HashMap simultaneously if it is used in multi-threaded environment. HashMap cannot be shared between several threads without appropriate synchronization as the HashMap is non thread safe.

Iterator The enumerator returned by  Hashtable is a fail-safe in nature and  if any thread modifies the map structurally by adding or removing any element except Interator’s own remove() then it throws ConcurrentModificationException. But this is not a assured behavior and will be done by JVM  at best of its effort. Iterator returned by the HashMap is fail-fast in nature.
Performance The most important difference is performance. Hashtable is much slower than HashMap because of thread safety and synchronization if used in single threaded environment Whereas on the other hand HashMap does not include synchronization and thread safety, so it is much  faster than Hashtable. So if you do not need synchronization  and is to be only used by one thread then HashMap will do better than Hashtable in java.
Null keys Even a single null key and null value is not allowed in Hashtable. Only one null key and any number of null values are allowed in HashMap.
Collection Framework Initially it was not the element of collection framework but after being retrofitted to implement the map interface it has been made a collection framework component later Hashmap  is a part of collection framework since the beginning as it implements map interface.
Extended Class Both Hashtable and HashMap though implement map interface but they extends two different classes. Hashtable extends Dictionary class which is the legacy class in java. Whereas on the other hand AbstractMap is extended by HashMap.
Legacy Class Hashtable is a legacy class. It is almost considered as due for deprecation since JDK 1.5, ConcurrentHashMap is considered as better option than the Hashtable HashMap is not a legacy class.
Usage As mentioned  above synchronization is the main difference between HashMap and Hashtable. Hashtable can  be used whenever there is a need of thread safe operation as all its methods are synchronized but it’s a legacy class and should  be avoided as there is nothing much about it, which  cannot be done by Hashmap. And one more thing is that synchronization operation  provides poor performance so it should  be avoided until and unless required. For multithreaded environment it is suggested  to use ConcurrentHashMap or even  you can synchronize  HashMap explicitly using Collections.synchronizedMap(hashmap) method.

Below example show the implementation of Hashtable and HashMap in Java

package com;

import java.util.*;
import java.lang.*;
import java.io.*;

public class Sample {
	public static void main(String args[]) {
		Hashtable<Integer,String> niz=new Hashtable<Integer,String>();
		
		niz.put(101,"neeraj");
		niz.put(101,"lata");
		niz.put(102,"rupanshi");
		niz.put(103,"nishu");
		niz.put(102,"chitranjan");
		
		System.out.println("Hashtable sample");
		
		for(Map.Entry m:niz.entrySet()) {
			System.out.println(m.getKey()+" "+m.getValue());
		}
		
		HashMap<Integer,String> niz1=new HashMap<Integer,String>();
		
		niz1.put(100,"amit");
		niz1.put(104,"gireesh");
		niz1.put(101,"gireesh");
		niz1.put(102,"sonia");
		niz1.put(105,"sonia");
		
		System.out.println("\nHashMap sample");
		
		for(Map.Entry m:niz1.entrySet()) {
			System.out.println(m.getKey()+" "+m.getValue());
		}
	}
}

 

Output

Hashtable sample
103 nishu
102 chitranjan
101 lata

HashMap sample
100 amit
101 gireesh
102 sonia
104 gireesh
105 sonia

Comment below if you found anything incorrect or have queries regarding above tutorial for difference between Hashtable and HashMap in Java.

Leave a Comment

Your email address will not be published. Required fields are marked *