group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
07 Feb 2019

What is difference between HashMap and Hash Table?

HashMap and HashTable in java are two important data structures in the Collection Framework which have some common things between them. Both implement Map interface. Both store the data in the form of key-value pairs. Both use Hashing technique to store the elements. But, there also exist significant differences between them. One important difference being the thread safety. HashMap is not thread safe where as HashTable is thread safe. In this post, we will discuss the differences and similarities between HashMap Vs HashTable in java.

Differences Between HashMap And HashTable In Java :

1) Thread Safe

HashTable is internally synchronized. Therefore, it is very much safe to use HashTable in multi threaded applications. Where as HashMap is not internally synchronized. Therefore, it is not safe to use HashMap in multi threaded applications without external synchronization. You can externally synchronize HashMap using Collections.synchronizedMap() method.

2)Null Keys And Null Values

HashMap allows maximum one null key and any number of null values. Where as HashTable doesn’t allow even a single null key and null value.

3) Traversal

HashMap returns only Iterators which are used to traverse over the elements of HashMap. HashTable returns Iterator as well as Enumeration which can be used to traverse over the elements of HashTable.

4) Performance

As HashTable is internally synchronized, this makes HashTable slightly slower than the HashMap.

5) When To Use What?

HashMap is always recommended if you don’t want thread safety. If you want thread safety, use either ConcurrentHashMap or make HashMap thread safe by using external synchronization through Collections.synchronizedMap() method. HashTable is not always recommended to use as it is considered as a legacy class.

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
2mos ago

HashMap and HashTable both are used to store data in key and value form.Both are using hashing techniques to store unique keys.But there are many differences between HashMap and HashTable classes that are given:

1.HashMap:It is non synchronized.It is not thread safe and can't be Shared between many threads without proper synchronization code.

HashTable:It is synchronized.It is thread safe and can be Shared with many threads.

2.HashMap:It allows one null key and multiple null values.

HashTable:It doesn't allow any null key or value.

3.HashMap:This is a new class introduced in JDK 1.2 and fast

HashTable:It is legacy class and slow

4.HashMap:We can make the HashMap as synchronized by calling this code Map m=Collections.synchronizedMap(hasjMap);

HashTable: This is internals synchronized and can't be unsynchronized.

5.HashMap:This inherits AbstractMap class and is traversed by Iterator which is fail -fast.

HashTable: This inherits Dictionary class and is traversed by Enumerator and Iterator where the Enumerator is not fail -fast.

Archana Priyadarshini
Archana Priyadarshini
Computer Science
2mos ago

1.HashMap is non-synchronized. This means if it’s used in multithread environment then more than one thread can access and process the HashMap simultaneously.

Hashtable is synchronized. It ensures that no more than one thread can access the Hashtable at a given moment of time. The thread which works on Hashtable acquires a lock on it to make the other threads wait till its work gets completed.

2.HashMap allows one null key and any number of null values.

Hashtable doesn’t allow null keys and null values.

3.HashMap implementation LinkedHashMap maintains the insertion order and TreeMap sorts the mappings based on the ascending order of keys.

Hashtable doesn’t guarantee any kind of order. It doesn’t maintain the mappings in any particular order.

4.Initially Hashtable was not the part of collection framework it has been made a collection framework member later after being retrofitted to implement the Map interface.

HashMap implements Map interface and is a part of collection framework since the beginning.

Shruthi Pillai
Shruthi Pillai
Information Technology
2mos ago

The HashMap class is roughly equivalent to Hashtable, except that it is non-synchronized and permits nulls. 

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 can not be shared between multiple threads without proper synchronization 

HashMap does not guarantee that the order of the map will remain constant over time