ConcurrentHashMap allow simultaneous access to the map. HashTables also offers synchronized access to the map, but your entire card is locked for any operation.
The logic of ConcurrentHashMap is your entire table is not getting locked , but only part of [ segments ]. Each segment manages its own HashTable. The lock applies only for updates. In case of exemptions, it allows the use of full concurrency.
Let four streams simultaneously work on a card with a capacity of 32, the table is divided into four segments, where each segment controls the hash table of the capacity. The collection contains a list of 16 segments by default, each of which is used to protect (or block) one bucket of cards.

This effectively means that 16 threads can modify the collection at a time. This concurrency level can be increased by using the optional concurrencyLevel constructor argument.
public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
As another answer said, ConcurrentHashMap offers a new putIfAbsent() method that is similar to put, except that the value will not be overridden if the key exists.
private static Map<String,String> aMap =new ConcurrentHashMap<String,String>(); if(!aMap.contains("key")) aMap.put("key","value");
The new method is also faster because it avoids double traversing as described above. contains method must find the segment and iterate over the table to find the key, and again the put method must cross the bucket and place the key.
Thalaivar Sep 27 '13 at 23:35 2013-09-27 23:35
source share