12 Difference Between HashMap And HashTable

SHARE

What Is HashMap?

HashMap denoted as HashMap<Key, Value> or HashMap<K, V>. It is the advanced version of HashTable and was introduced as a new class in JDK 1.2. It is a part of Java Collections. It provides the basic implementation of the Map interface of Java. It stores the data in (key, Value) pairs and you can access them by an index of another type (e.g an integer). One object is used as a key (index) to another object (value).

A HashMap uses a technique referred to as ‘’Hashing’’. In Hashing, a longer string is converted into a shorter string by applying some algorithm or ‘hash function’. A string is converted to a shorter string as it helps in searching that it faster. It is used for efficient indexing.

HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This makes no guarantees as to the order of the Map. To use this class and its methods, you need to import java.util.HashMap package or its superclass.  

What You Need To Know About HashMap

  • HashMap is the advanced version of HashTable and was introduced as a new class in JDK 1.2. It is a part of Java Collections.  
  • It implements Map and Serializable interface.
  • Synchronization is not implemented in HashMap and is not thread safe, therefore, can’t be shared between many threads without proper synchronization. 
  • HashMap inherits AbstractMap class.
  • HashMap provides Iterator for its iteration in order to traverse the values stored in it.
  • Internal implementation of both classes is same up to certain extent. However, in case of HashMap one null key and multiple null values are allowed.
  • Due to the absence of synchronization HashMap is faster when compared to HashTable and is preferred when synchronization is not required.
  • HashMap uses balanced Tree as collision resolution strategy which has bounding search time of O(log n).
  • Iterator is fail-fast and it throws a concurrent Modification Exception if any other thread tries to modify the map.
  • HashMap does not maintain insertion order in Java.
  • The initial default capacity of a HashTable in the HashMap class is 16 and the default (initial) load factor is 0.75

HashTable

A HashTable is a data structure which stores data in an associative manner. In a HashTable, data is stored in an array format where each value has its own unique index value. It uses a hash function to compute an index into an array in which an element will be inserted or searched.

 In HashTable insertion and search operations are very fast irrespective of the size of the data. HashTable uses an array as a storage medium and uses HashTable uses an array as storage medium and uses hash technique to generate and index where an element is to be inserted or is to be located from.

In Java, the HashTable is implemented by the ‘HashTable’ class. This class implements the Map interface and inherits the dictionary class.

What You Need To Know About HashTable

  • HashTable is a legacy class and was introduced prior HashMap, it was not part of the initial Java collections.
  • It implements the collection framework.
  • HashTable is synchronized and is thread safe therefore it can be shared with many threads.
  • HashTable inherits Dictionary class.
  • Iterator HashTable also provides Enumerator to traverse the values stored in it.
  • HashTable is internally implemented in such manner that it does not allow any null key or any null value.
  • Synchronization in HashTable makes it slower when compared to HashMap but also eliminate the writing of extra code to obtain the synchronization. 
  • HashTable uses separate chaining (with linked lists) as collision handling strategy bounding search time of O(n).
  • The enumerator is not fail-fast. If the HashTable is modified during traversing then it will not throw any error.
  • HashTable does not maintain insertion order in java.
  • The Default capacity of HashTable is 11.

Difference Between HashMap And HashTable In Tabular Form

BASIS OF COMPARISON HASHMAP HASHTABLE
Description HashMap is the advanced version of HashTable and was introduced as a new class in JDK 1.2. It is a part of Java Collections.    HashTable is a legacy class and was introduced prior HashMap, it was not part of the initial Java collections.  
Function It implements Map and Serializable interface.   It implements the collection framework.  
Synchronization Synchronization is not implemented in HashMap and is not thread safe, therefore, can’t be shared between many threads without proper synchronization.    Synchronization is not implemented in HashMap and is not thread safe, therefore, can’t be shared between many threads without proper synchronization.   
Inheritance HashMap inherits AbstractMap class.   HashTable inherits Dictionary class.  
Traversal Of Values HashMap provides Iterator for its iteration in order to traverse the values stored in it.   Iterator HashTable also provides Enumerator to traverse the values stored in it.  
Internal Implementation Internal implementation of both classes is same upto certain extent. However, in case of HashMap one null key and multiple null values are allowed.   HashTable is internally implemented in such manner that it does not allow any null key or any null value.  
Speed Due to the absence of synchronization HashMap is faster when compared to HashTable and is preferred when synchronization is not required.   Synchronization in HashTable makes it slower when compared to HashMap but also eliminate the writing of extra code to obtain the synchronization.   
Bounding Search Time HashMap uses balanced Tree as collision resolution strategy which has bounding search time of O(log n).   HashTable uses separate chaining (with linked lists) as collision handling strategy bounding search time of O(n).  
Concurrent Modification Exception Iterator is fail-fast and it throws a concurrent Modification Exception if any other thread tries to modify the map.   The enumerator is not fail-fast. If the HashTable is modified during traversing then it will not throw any error.  
Insertion Order HashMap does not maintain insertion order in Java.   HashTable does not maintain insertion order in java.  
Default Capacity The default capacity of a HashTable in the HashMap class is 16.   The Default capacity of HashTable is 11.