Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 12 October 2016

What is ConcurrentMap In Java ?

Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed about
What is ConcurrentNavigableMap In Java ?
Here we learn what is ConcurrentMap in java.But before starting this you must know What is Map Interface in Java ?
Interface ConcurrentMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values

All Superinterfaces:
Map<K,V>

All Known Subinterfaces:

All Known Implementing Classes:
ConcurrentHashMap, ConcurrentSkipListMap

public interface ConcurrentMap<K,V>
extends Map<K,V>

ConcurrentMap interface represents a Map which is capable of handling concurrent access to it.
ConcurrentMap has a few extra atomic methods in addition to the methods it inherits from its superinterface i.e Map interfcae.
ConcurrentMap is an interface so you need to use one of its implementation class in order to use it.

This interface is a member of the Java Collections Framework.

Method Summary of ConcurrentMap:

Modifier and Type
Method Name
Description
default V
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
default V
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
default V
computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
default void
forEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
default V
getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
default V
merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
V
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value.
boolean
remove(Object key, Object value)
Removes the entry for a key only if currently mapped to a given value.
V
replace(K key, V value)
Replaces the entry for a key only if currently mapped to some value
boolean
replace(K key, V oldValue, V newValue)
Replaces the entry for a key only if currently mapped to a given value.
default void
replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

Example of ConcurrentMap :-

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ConcurrentMapExample {

public static void main(String[] args) {
ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
map.put("A", "Apple");
System.out.println(map.get("A"));
}

}

Program Output :-

Apple


Share this Blog with yours Friends !!

No comments:

Post a Comment