Both the ConcurrentHashMap and Hashtable doesn't allow any null key and allows concurrent access.
- ConcurrentHashMap in Java is introduced as an alternative of Hashtable in Java, which is a synchronized collection
class(thread-safe). It provides full concurrency of retrievals (as
the get operation does not entail locking) and high expected
concurrency of updates.
- The Hashtable class is mostly considered obsolete. It is a
thread-safe hash map, but unlike ConcurrentHashMap, all its methods
are simply synchronized, which means that all operations on this map
block, even retrieval of independent values
Ex:-
Map<Integer, String> ch = new ConcurrentHashMap<Integer, String>();
ch.put(1, "First");
ch.put(2, "Second");
ch.put(null, "value"); //throws java.lang.NullPointerException
ch.forEach((k,v) -> System.out.println("Key = " + k + ", Value = " + v));
For the difference between HashMap and ConcurrentHashMap refer this.
Helpful thread from Stack Overflow found here.