September 2019
Intermediate to advanced
816 pages
18h 47m
English
The thread-safe version of a Map is ConcurrentHashMap.
The following table enumerates the Java built-in single-thread and multithreaded maps:
|
Single thread |
Multithreaded |
|
HashMap TreeMap (sorted keys) LinkedHashMap (maintain insertion order) IdentityHashMap (keys compared via ==) WeakHashMap EnumMap |
ConcurrentHashMap ConcurrentSkipListMap (sorted map) Hashtable |
ConcurrentHashMap allows retrieval operations (for example, get()) without blocking. This means that retrieval operations may overlap with update operations (including put() and remove()).
Creating a ConcurrentHashMap can be done as follows:
ConcurrentMap<Integer, Integer> map = new ConcurrentHashMap<>();