ConcurrentHashMap
Take a look at ConcurrentHashMap in Java.
We'll cover the following...
- Differences between a ConcurrentHashMap and SynchronizedMap
- Creating a ConcurrentHashMap
- Using the no-arg constructor
- Using the constructor that takes initial capacity
- Using the constructor that takes initial capacity and load factor
- Using the constructor that takes another Map as a parameter
- Using the constructor that takes initial capacity, load factor, and concurrency level as a parameter
- Inserting into a ConcurrentHashMap
We can make our Map thread-safe by using the synchronizedMap()
method of the Collections class. This method returns SynchronizedMap in which all the methods are synchronized. There are lots of differences between a ConcurrentHashMap and a SynchronizedMap, which we will discuss next.
Although all the basic operations such as insert, fetch, replace, and remove in a ConcurrentHashMap are similar to the HashMap. But we will discuss them so that you are not confused about how to perform these operations in a ConcurrentHashMap.
Differences between a ConcurrentHashMap and SynchronizedMap
Let’s discuss some of the differences between a ConcurrentHashMap and a SynchronizedMap.
-
In a SynchronizedMap, the entire Map is ...