Tuesday, November 27, 2012

Similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap

There are several similarities and differences between HashMap, Hashtable, ConcurrentHashMap and synchronizedMap  in Java. (I name synchronizedMap the object retrieved by Collections.synchronizedMap(map) method.)


Comparing HashMap, Hashtable and synchronizedMap:
- Hashtable does not allow null keys or values. HashMap and synchronizedMap allow one null key and any number of null values.
- Hashtable and synchronizedMap are synchronized, whereas HashMap is not. If you have multithreaded application and you use HashMap, you may have to do the synchronization manually.
- Iterators returned by HashMap, Hashtable and synchronizedMap are fail-fast, so you have to synchronize its manipulation manually to avoid ConcurrentModificationException. (Note that Hashtable has no iterator() method, but you could get a Iterator indirectly by calling Hashtable.values().iterator() or Hashtable.entrySet().iterator() methods.) In many cases using ConcurrentHashMap is the better decision (its iterators are fail-safe).

Featuring ConcurrentHashMap:
- Hashtable is synchronious, ConcurrentHashMap is just thread safe.
- ConcurrentHashMap as Hashtable could not take null as a key.
- Hashtable is interchangeable by ConcurrentHashMap in source code (so replacing it will not make compile time errors).
- ConcurrentHashMap could block only some parts of it, to avoid the costly full blocking.
- Iterators returned by ConcurrentHashMap are fail-safe, so they are not guaranteed to be actual in the exact moment they are created (they could had been valid in earlier moment).
- ConcurrentHashMap: Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset.
- ConcurrentHashMap: For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries.
- ConcurrentHashMap: Multiple reads can almost always execute concurrently, simultaneous reads and writes can usually execute concurrently, and multiple simultaneous writes can often execute concurrently.
- ConcurrentHashMap is available in Java 1.5

Compound operations:
- HashMap, Hashtable and synchronizedMap : if you need full synchronization, compound operations have to be synchronized manually. For example operations like "put-if-absent", "ad-hoc iteration" or just normal iteration;
- ConcurrentHashMap and compound operations: if you use ConcurrentHashMap, synchronization is never garanted, you could not syncronize compound operations

Useful links:
A good article for ConcurrentHashMap could be found in this sites:
http://www.codercorp.com/blog/java/why-concurrenthashmap-is-better-than-hashtable-and-just-as-good-hashmap.html
http://ria101.wordpress.com/2011/12/12/concurrenthashmap-avoid-a-common-misuse/

1 comment: