What is the difference between HashMap
and Hashtable
? According to what I know about data structures, it would be the same conceptually, but in Java , what would be the difference?
Which is more recommended to be used in applications that use Threads
?
Synchronization
The main difference between one and the other is the internal synchronization of the object.
For multi-threaded applications it is preferable to choose
Hashtable
overHashMap
, which has no synchronization.You must take into account, however, that it
HashTable
offers synchronization in the access and mutation methods, which will prevent two different threads from concurrently adding or deleting from the list, but there are typical operations of a multi-threaded application that will require external sync .A common case is check-and-add , which is nothing more than looking to see if a key exists in the list, and adding it if not. There is no way to do this in an atomic operation using
Hashtable
niHashMap
.Iteration over entries of
Hashtable
is also not thread-safe , unless you prevent the list from being modified with additional synchronization:There are Interface Implementations
ConcurrentMap
(for exampleConcurrentHashMap
) that solve some of these problems by including check-then-act semantics that is thread-safe , for exampleNull keys or values
Another important difference is that
Hashtable
it does not allow keys or valuesnull
, while itHashMap
allows a key and any number of valuesnull
.iteration order
One of the subclasses of
HashMap
isLinkedHashMap
, which is useful in the case that you require a predictable iteration order (which defaults to insertion order). You can easily change your declaration fromHashMap
toLinkedHashMap
.The typical interview question
HashTable is a bit old data structure, although everything they say about synchronization is true and that it does not allow a null as key or value, I think it is worth mentioning that its hierarchy line is different from that of a map since HastTable extends Dictionary another slightly old-fashioned data structure, while a HashMap extends AbstractMap but both implement Map.
There is a method in the Collections class (Helper Class), to create synchronized maps that can help us replace the use of HashTables in our programs.
You can also achieve synchronization with a synchronized block
Source: Java Doc Java - grepcode.com
To understand a little better how these objects work, I highly recommend reading their source code:
HashMap source code: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/HashMap.java
HashTable source code: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/util/Hashtable.java#Hashtable
If you want to use a synchronized map for a Multi-Threading application there is also this object in the java.util concurrency package that can be quite useful.
There are several differences between the two:
Hashtable
it is synchronized,HashMap
no. In your case you must useHashtable
since your application is multithreaded as you mention.Hashtable
does not allownull
as key or value .HashMap
it only allows a single key to benull
, and there are no restrictions on using itnull
on as many values as you want.One of the subclasses of
HashMap
isLinkedHashMap
, so if you wanted a predictable order when iterating over it you can change itHashMap
toLinkedHashMap
.Hashtable
it has no similar subclass.