In this article, you will learn-
Java ConcurrentMap Interface
In this tutorial, we will learn about the Java ConcurrentMap interface and its strategies.
The ConcurrentMap interface of the Java collections framework gives a thread-safe map. That is, different strings can get to the map immediately without influencing the consistency of sections on a map.
ConcurrentMap is known as a synchronized map.
It extends the Map interface.
The class that executes ConcurrentMap
Since ConcurrentMap is an interface, we can’t make objects from it.
To use the functionalities of the ConcurrentMap interface, we have to use the class ConcurrentHashMap that actualizes it.
How to use ConcurrentMap?
To use the ConcurrentMap, we should import the java.util.concurrent.ConcurrentMap package first. When we import the package, here’s how we can create a concurrent map.
// ConcurrentMap implementation by ConcurrentHashMap
CocurrentMap<Key, Value> numbers = new ConcurrentHashMap<>();
In the above code, we have created a concurrent map named numbers.
Here,
- Key – a unique identifier used to associate each element (value) in a map
- Value – elements associated by keys in a map
Methods of ConcurrentMap
The ConcurrentMap interface incorporates all the techniques for the Map interface. It is on the grounds that Map is the super interface of the ConcurrentMap interface.
Other than each one of those techniques, here are the strategies explicit to the ConcurrentMap interface.
- putIfAbsent() – Inserts the specified key/value to the map if the specified key is not already associated with any value.
- compute() – Computes an entry (key/value mapping) for the specified key and its previously mapped value.
- computeIfAbsent() – Computes a value using the specified function for the specified key if the key is not already mapped with any value.
- computeIfPresent() – Computes a new entry (key/value mapping) for the specified key if the key is already mapped with the specified value.
- forEach() – Access all entries of a map and perform the specified actions.
- merge() – Merges the new specified value with the old value of the specified key if the key is already mapped to a certain value. If the key is not already mapped, the method simply associates the specified value to our key.
To learn more, visit Java ConcurrentMap (official Java documentation).
Implementation of ConcurrentMap in ConcurrentHashMap
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
class Main {
public static void main(String[] args) {
// Creating ConcurrentMap using ConcurrentHashMap
ConcurrentMap<String, Integer> numbers = new ConcurrentHashMap<>();
// Insert elements to map
numbers.put("Two", 2);
numbers.put("One", 1);
numbers.put("Three", 3);
System.out.println("ConcurrentMap: " + numbers);
// Access the value of specified key
int value = numbers.get("One");
System.out.println("Accessed Value: " + value);
// Remove the value of specified key
int removedValue = numbers.remove("Two");
System.out.println("Removed Value: " + removedValue);
}
}
Output
ConcurrentMap: {One=1, Two=2, Three=3}
Accessed Value: 1
Removed Value: 2
To learn more about ConcurrentHashMap, visit Java ConcurrentHashMap.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.