English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input output (I/O)

Java Reader/Writer

Java other topics

Java ConcurrentMap interface

In this tutorial, we will learn about the Java ConcurrentMap interface and its methods.

The ConcurrentMap interface in the Java collection framework provides thread-safe mappings. That is, multiple threads can access the mapping at once without affecting the consistency of the entries in the mapping.

ConcurrentMap is called a synchronized map.

It inheritsMap interface.

Classes that implement ConcurrentMap

Since ConcurrentMap is an interface, it is not possible to create an object from it.

To use the functionality of the ConcurrentMap interface, we need to use the class ConcurrentHashMap that implements the interface.

How to use ConcurrentMap?]}

To use ConcurrentMap, we must first import the java.util.concurrent.ConcurrentMap package. After importing the package, we will create a concurrent map as follows.

// Usage of ConcurrentHashMap class
ConcurrentMap<Key, Value> numbers = new ConcurrentHashMap<>();

In the above code, we created a ConcurrentMap named numbers.

Here,

  • Key - Used as the unique identifier to associate each element (value) in the map

  • Value - The element associated with the key in the map

Methods of ConcurrentMap

The ConcurrentMap interface includes all methods of the Map interface. This is because Map is the superinterface of the ConcurrentMap interface.

In addition to all these methods, there are methods specific to the ConcurrentMap interface.

  • putIfAbsent() - If the specified key has not yet been associated with any value, then the specified key/The value is inserted into the map.

  • compute() - Calculate the entry (key/Value mapping).

  • computeIfAbsent() - If the key has not yet been mapped to any value, calculate a value for the specified key using the specified function.

  • computeIfPresent() - If the specified key has already been mapped to a value, calculate a new entry (key/Value mapping).

  • forEach() - Access all entries in the map and perform the specified operation.

  • merge() -If the specified key is already mapped to a value, the specified new value is merged with the old value of the specified key. If the key is not yet mapped, this method associates the specified value with the key.

The implementation of ConcurrentMap in ConcurrentHashMap

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
class Main {
    public static void main(String[] args) {
        //Create a ConcurrentMap using ConcurrentHashMap
        ConcurrentMap<String, Integer> numbers = new ConcurrentHashMap<>();
        // Insert elements into the map
        numbers.put("Two", 2);
        numbers.put("One", 1);
        numbers.put("Three", 3);
        System.out.println("ConcurrentMap: ", + numbers);
        //Access the specified key
        int value = numbers.get("One");
        System.out.println("Accessed value: ", + value);
        //Delete the value of the specified key
        int removedValue = numbers.remove("Two");
        System.out.println("Deleted value: ", + removedValue);
    }
}

Output result

ConcurrentMap: {One=1, Two=2, Three=3}
Accessed value: 1
Deleted value: 2

To learn more about ConcurrentHashMap, please visitJava ConcurrentHashMap.