Name

ConcurrentMap<K,V>

Synopsis

This interface extends the java.util.Map interface to add four important atomic methods. As with the Map interface, the type variables K and V represent the types of the mapped keys and values.

putIfAbsent( ) atomically tests whether a key is already defined in the map, and if not, maps it to the specified value. remove( ) atomically removes the specified key from the map, but only if it is mapped to the specified value. It returns true if it modified the map. There are two versions of the atomic replace( ) method. The first checks whether the specified value is already mapped to a value. If so, it replaces the existing mapping with the specified value and returns true. Otherwise, it returns false. The three-argument version of replace( ) maps the specified key to the specified new value, but only if the key is currently mapped to the specified old value. It returns true if the replacement was made and false otherwise.

java.util.concurrent.ConcurrentMap<K,V>

Figure 16-77. java.util.concurrent.ConcurrentMap<K,V>

public interface ConcurrentMap<K,V> extends java.util.Map<K,V> {
// Public Instance Methods
     V putIfAbsent(K key, V value);  
     boolean remove(Object key, Object value);  
     V replace(K key, V value);  
     boolean replace(K key, V oldValue, V newValue);  
}

Implementations

ConcurrentHashMap

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.