Difference between HashMap and Hashtable in java

In this post, we will see the difference between HashMap and Hashtable in Java. First, we will see HashMap vs Hashtable in brief later we will see each point in details.

Difference between HashMap and Hashtable in java

HashMapHashtable
1.HashMap class implements Map interface and extends AbstractMap class.1.Hashtable class implements Map interface and extends Dictionary class.
2.HashMap introduced in jdk1.2.2.Hashtable introduced in jdk1.0 as legacy class(Don't get confused Legacy classes and interfaces introduced in the earlier versions of Java like Vector/Hashtable etc).
3.HashMap is not-thread safe because all methods inside HashMap are non synchronized.3.Hashtable is thread safe because all methods of Hashtable are synchronized
4.HashMap has initial capacity 16 and load factor is 0.75f.4.Hashtable has intial cappacity 11 and load factor is 0.75f.
5.HashMap is fast.5.Hashtable is slow
6. HashMap can have one null as key and multiple null values.6.Hashtable doesnot allow null as key or value.

 

 

HashMap class implements Map interface and extends AbstractMap class whereas Hashtable class implements Map interface and extends Dictionary class.

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
 
}

 

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {

}

 

 

 

HashMap methods are not synchronized whereas all methods of Hashtable is synchronized. Let’s see put() method for HashMap and Hashtable.

 public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }
    public synchronized V put(K key, V value) {
        // Make sure the value is not null
        if (value == null) {
            throw new NullPointerException();
        }

        // Makes sure the key is not already in the hashtable.
        Entry<?,?> tab[] = table;
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        @SuppressWarnings("unchecked")
        Entry<K,V> entry = (Entry<K,V>)tab[index];
        for(; entry != null ; entry = entry.next) {
            if ((entry.hash == hash) && entry.key.equals(key)) {
                V old = entry.value;
                entry.value = value;
                return old;
            }
        }

        addEntry(hash, key, value, index);
        return null;
    }

Hashtable put method is synchronized. Similarly, if you look into all other methods of Hashtable, those all are synchronized.

 

HashMap has initial capacity 16 and load factor is 0.75f whereas Hashtable has initial capacity 11 and load factor is 0.75f.

Let’s see how initial capacity has been defined in HashMap class.

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {

    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;// this is nothing but 16
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // 
    }

    // more code
}

 

In case of Hashtable  –

public class Hashtable<K,V>
    extends Dictionary<K,V>
    implements Map<K,V>, Cloneable, java.io.Serializable {    

    public Hashtable(int initialCapacity) {
        this(initialCapacity, 0.75f);
    }

    /**
     * Constructs a new, empty hashtable with a default initial capacity (11)
     * and load factor (0.75).
     */
    public Hashtable() {
        this(11, 0.75f);
    }
    // more code
}

 

HashMap is fast whereas Hashtable is slow.

Let’s see an example which illustrates how HashMap is faster than Hashtable.

package hashmap;
 
import java.util.*;
 
//import org.joda.time.LocalDate;
 
public class Test2 {
 
public static void main(String[] args) {
	
	    // define ArrayList Object
	    Map<Integer, String> hashmapObject = new HashMap<>();
		
		//get current time in milliseconds
		long l1 = System.currentTimeMillis();
		for(int i=0; i<1000000; i++) {
			//add ram 1M times
			hashmapObject.put(2, "ram");
		}
		//get current time in milliseconds
		long l2 = System.currentTimeMillis();
		
		
		//l2 - l1 will return time took to add 1M times ram in listOfArrayList
		System.out.println("time taken by HashMap "+(l2-l1));
		
		long l3 = System.currentTimeMillis();
		Map<Integer, String> hashtableObject = new Hashtable<>();
		for(int i=0; i<1000000; i++) {
			hashtableObject.put(2,"ram");
		}
		
		
		long l4 = System.currentTimeMillis();
		
		//l2 - l1 will return time took to add 1M times ram in listOfVector
		System.out.println("time taken by Hashtable "+(l4-l3));
		
		
	
	
	
 
}
 
}

 

Output is –

time taken by HashMap 21
time taken by Hashtable 54

That’s all about Difference between HashMap and Hashtable in java.

You may like.

See HashMap and Hashtable docs.