Differences between HashMap and Hash-table
  •  Synchronization or Thread Safe : One of the major differences between HashMap and Hashtable is that HashMap is non-synchronized whereas Hashtable is synchronized, which means Hashtable is thread-safe and can be shared between multiple threads but HashMap cannot be shared between multiple threads without proper synchronization. Java 5 introduced ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java.Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
  • Null keys and null values : The HashMap class is roughly equivalent to Hashtable, except that it permits nulls. (HashMap permits one null key and multiple null values.
    Hashtable doesn't permit any sort of nulls (key or values).).
  • Iterating the values: The third significant difference between HashMap vs Hashtable is that Iterator in the HashMap is a fail-fast iterator while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator’s own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort. This is also an important difference between Enumeration and Iterator in Java.
  • Default Capacity: 
  •  Hashmap: static final int DEFAULT_INITIAL_CAPACITY =16  static final float DEFAULT_LOAD_FACTOR = 0.75f;  
  • Hashtable:  static final int DEFAULT_INITIAL_CAPACITY = 11;   static final float DEFAULT_LOAD_FACTOR = 0.75f;
  • HashMap does not guarantee that the order of the map will remain constant over time.
HashMap can be synchronized by using Collections.synchronizedMap() method
Map m = Collections.synchronizedMap(hashMap);
Example:

import java.util.HashMap;

import java.util.Hashtable;

public class HashMapHashtableExample {
   
    public static void main(String[] args) {      

 

        Hashtable<String,String> hashtableobj = new Hashtable<String, String>();
        hashtableobj.put("name", "ramu");
        hashtableobj.put("petname", "ram");
        System.out.println("Hashtable object output :"+ hashtableobj);



        HashMap hashmapobj = new HashMap();
        hashmapobj.put("name", "ramu"); 
        hashmapobj.put("pertname", "ram");
        System.out.println("HashMap object output :"+hashmapobj);

 }
}


hashmap vs hashtable

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

2 comments for Differences between HashMap and Hash-table

  1. Very useful detailed information on HashMap and Hash-table. especially good and to the point code included in this blog. I am searching such Java questions and answers on google, I found your blog.

    Thanks for helping me.

    ReplyDelete
  2. this is helpful to me.
    thank u.

    ReplyDelete

Select Menu