public interface ConcurrentMap
implements Map<K, V>
java.util.concurrent.ConcurrentMap<K, V> |
Known Indirect Subclasses
|
A Map
提供线程安全性和原子性保证。
为了维护指定的保证,必须通过此接口的实现覆盖putIfAbsent(K, V)
继承自Map
的方法(包括putIfAbsent(K, V)
缺省实现。 类似的,集合的实现返回的方法keySet()
, values()
,并entrySet()
必须覆盖的方法,如removeIf
必要时保留原子性保证。
存储器一致性效果:当与其他并发集合,事先将物体放置成在一个线程动作 ConcurrentMap
作为键或值 happen-before个行动随后到该对象的距离的访问或移除 ConcurrentMap
在另一个线程。
Public methods |
|
---|---|
default V |
compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 尝试计算指定键和其当前映射值的映射(如果没有当前映射, |
default V |
computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) 如果指定的键尚未与值关联(或映射到 |
default V |
computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) 如果指定键的值存在且非空,则尝试计算给定键和其当前映射值的新映射。 |
default void |
forEach(BiConsumer<? super K, ? super V> action) 对此映射中的每个条目执行给定操作,直到处理完所有条目或操作抛出异常为止。 |
default V |
getOrDefault(Object key, V defaultValue) 返回指定键映射到的值,如果此映射不包含键映射,则返回 |
default V |
merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) 如果指定的键尚未与某个值关联或者与null关联,则将其与给定的非空值相关联。 |
abstract V |
putIfAbsent(K key, V value) 如果指定的键尚未与值相关联,则将其与给定值相关联。 |
abstract boolean |
remove(Object key, Object value) 只有在当前映射到给定值的情况下才删除键的条目。 |
abstract boolean |
replace(K key, V oldValue, V newValue) 仅当当前映射到给定值时才替换密钥的条目。 |
abstract V |
replace(K key, V value) 仅当当前映射到某个值时才替换密钥的条目。 |
default void |
replaceAll(BiFunction<? super K, ? super V, ? extends V> function) 用对该条目调用给定函数的结果替换每个条目的值,直到处理完所有条目或者该函数抛出异常。 |
Inherited methods |
|
---|---|
From interface java.util.Map
|
V compute (K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
尝试计算指定键和其当前映射值的映射(如果没有当前映射, null
)。 例如,要将String
味精创建或附加到值映射:
map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
(Method
merge()
is often simpler to use for such purposes.)
如果重映射函数返回null
,则映射被移除(或者如果最初不存在则保持不存在)。 如果重映射函数本身抛出一个(未经检查的)异常,则重新抛出异常,并且当前映射保持不变。
重新映射函数不应该在计算过程中修改此映射。
map
: for (;;) {
V oldValue = map.get(key);
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null) {
if ((oldValue != null)
? map.replace(key, oldValue, newValue)
: map.putIfAbsent(key, newValue) == null)
return newValue;
} else if (oldValue == null || map.remove(key, oldValue)) {
return null;
}
}
When multiple threads attempt updates, map operations and the remapping function may be called multiple times. 此实现假定ConcurrentMap不能包含空值, get()
返回null的明确表示该键不存在。 支持空值的实现必须覆盖此默认实现。
Parameters | |
---|---|
key |
K : key with which the specified value is to be associated |
remappingFunction |
BiFunction : the remapping function to compute a value |
Returns | |
---|---|
V |
the new value associated with the specified key, or null if none |
Throws | |
---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
V computeIfAbsent (K key, Function<? super K, ? extends V> mappingFunction)
如果指定的键尚未与值关联(或映射到 null
),则尝试使用给定的映射函数计算其值,并将其输入到此映射中,除非 null
。
如果映射函数返回null
,则不记录映射。 如果映射函数本身抛出(未经检查的)异常,则重新抛出异常,并且不记录映射。 最常见的用法是构造一个新的对象作为初始映射值或记忆结果,如下所示:
map.computeIfAbsent(key, k -> new Value(f(k)));
或者实现一个多值映射, Map<K,Collection<V>>
,每个键支持多个值:
map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
计算过程中映射函数不应修改此映射。
map
: V oldValue, newValue;
return ((oldValue = map.get(key)) == null
&& (newValue = mappingFunction.apply(key)) != null
&& (oldValue = map.putIfAbsent(key, newValue)) == null)
? newValue
: oldValue;
此实现假定ConcurrentMap不能包含空值,并且get()
返回null意味着密钥不存在。 支持空值的实现必须覆盖此默认实现。
Parameters | |
---|---|
key |
K : key with which the specified value is to be associated |
mappingFunction |
Function : the mapping function to compute a value |
Returns | |
---|---|
V |
the current (existing or computed) value associated with the specified key, or null if the computed value is null |
Throws | |
---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
V computeIfPresent (K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
如果指定键的值存在且非空,则尝试计算给定键和其当前映射值的新映射。
如果重映射函数返回null
,则映射将被删除。 如果重映射函数本身抛出一个(未经检查的)异常,则重新抛出异常,并且当前映射保持不变。
重新映射函数不应该在计算过程中修改此映射。
map
: for (V oldValue; (oldValue = map.get(key)) != null; ) {
V newValue = remappingFunction.apply(key, oldValue);
if ((newValue == null)
? map.remove(key, oldValue)
: map.replace(key, oldValue, newValue))
return newValue;
}
return null;
When multiple threads attempt updates, map operations and the remapping function may be called multiple times. 此实现假定ConcurrentMap不能包含空值, get()
返回null的值明确表示该键不存在。 支持空值的实现必须覆盖此默认实现。
Parameters | |
---|---|
key |
K : key with which the specified value is to be associated |
remappingFunction |
BiFunction : the remapping function to compute a value |
Returns | |
---|---|
V |
the new value associated with the specified key, or null if none |
Throws | |
---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
void forEach (BiConsumer<? super K, ? super V> action)
对此映射中的每个条目执行给定操作,直到处理完所有条目或操作抛出异常为止。 除非实现类另有规定,否则按照条目集迭代的顺序执行操作(如果指定了迭代顺序)。操作抛出的异常会中继给调用者。
map
: for (Map.Entry<K,V> entry : map.entrySet()) {
action.accept(entry.getKey(), entry.getValue());
}
IllegalStateException
thrown by getKey()
or getValue()
indicates that the entry has been removed and cannot be processed. Operation continues for subsequent entries.Parameters | |
---|---|
action |
BiConsumer : The action to be performed for each entry |
Throws | |
---|---|
NullPointerException |
V getOrDefault (Object key, V defaultValue)
返回指定键映射到的值,或者如果此映射不包含键映射,则返回 defaultValue
。
get()
returning null unambiguously means the key is absent. Implementations which support null values must override this default implementation.Parameters | |
---|---|
key |
Object : the key whose associated value is to be returned |
defaultValue |
V : the default mapping of the key |
Returns | |
---|---|
V |
the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key |
Throws | |
---|---|
ClassCastException |
|
NullPointerException |
V merge (K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
如果指定的键尚未与某个值关联或者与null关联,则将其与给定的非空值相关联。 否则,将使用给定的重映射函数的结果替换关联的值,或者如果结果为null
,则将其删除。 组合键的多个映射值时,此方法可能会有用。 例如,要创建或附加一个String msg
到值映射:
map.merge(key, msg, String::concat)
如果重映射函数返回null
,则映射将被删除。 如果重映射函数本身抛出一个(未经检查的)异常,则重新抛出异常,并且当前映射保持不变。
重新映射函数不应该在计算过程中修改此映射。
map
: for (;;) {
V oldValue = map.get(key);
if (oldValue != null) {
V newValue = remappingFunction.apply(oldValue, value);
if (newValue != null) {
if (map.replace(key, oldValue, newValue))
return newValue;
} else if (map.remove(key, oldValue)) {
return null;
}
} else if (map.putIfAbsent(key, value) == null) {
return value;
}
}
When multiple threads attempt updates, map operations and the remapping function may be called multiple times. 此实现假定ConcurrentMap不能包含空值, get()
返回null的含义明确表示该键不存在。 支持空值的实现必须覆盖此默认实现。
Parameters | |
---|---|
key |
K : key with which the resulting value is to be associated |
value |
V : the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key |
remappingFunction |
BiFunction : the remapping function to recompute a value if present |
Returns | |
---|---|
V |
the new value associated with the specified key, or null if no value is associated with the key |
Throws | |
---|---|
UnsupportedOperationException |
|
ClassCastException |
|
NullPointerException |
|
IllegalArgumentException |
V putIfAbsent (K key, V value)
如果指定的键尚未与值相关联,则将其与给定值相关联。 这相当于,对于这个map
:
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);
except that the action is performed atomically.
Map
.Parameters | |
---|---|
key |
K : key with which the specified value is to be associated |
value |
V : value to be associated with the specified key |
Returns | |
---|---|
V |
the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.) |
Throws | |
---|---|
UnsupportedOperationException |
if the put operation is not supported by this map |
ClassCastException |
if the class of the specified key or value prevents it from being stored in this map |
NullPointerException |
if the specified key or value is null, and this map does not permit null keys or values |
IllegalArgumentException |
if some property of the specified key or value prevents it from being stored in this map |
boolean remove (Object key, Object value)
只有在当前映射到给定值的情况下才删除键的条目。 这相当于这个map
:
if (map.containsKey(key)
&& Objects.equals(map.get(key), value)) {
map.remove(key);
return true;
} else {
return false;
}
except that the action is performed atomically.
Map
.Parameters | |
---|---|
key |
Object : key with which the specified value is associated |
value |
Object : value expected to be associated with the specified key |
Returns | |
---|---|
boolean |
true if the value was removed |
Throws | |
---|---|
UnsupportedOperationException |
if the remove operation is not supported by this map |
ClassCastException |
if the key or value is of an inappropriate type for this map (optional) |
NullPointerException |
if the specified key or value is null, and this map does not permit null keys or values (optional) |
boolean replace (K key, V oldValue, V newValue)
仅当当前映射到给定值时才替换密钥的条目。 这相当于这个map
:
if (map.containsKey(key)
&& Objects.equals(map.get(key), oldValue)) {
map.put(key, newValue);
return true;
} else {
return false;
}
except that the action is performed atomically.
Map
.Parameters | |
---|---|
key |
K : key with which the specified value is associated |
oldValue |
V : value expected to be associated with the specified key |
newValue |
V : value to be associated with the specified key |
Returns | |
---|---|
boolean |
true if the value was replaced |
Throws | |
---|---|
UnsupportedOperationException |
if the put operation is not supported by this map |
ClassCastException |
if the class of a specified key or value prevents it from being stored in this map |
NullPointerException |
if a specified key or value is null, and this map does not permit null keys or values |
IllegalArgumentException |
if some property of a specified key or value prevents it from being stored in this map |
V replace (K key, V value)
仅当当前映射到某个值时才替换密钥的条目。 这相当于这个map
:
if (map.containsKey(key))
return map.put(key, value);
else
return null;
except that the action is performed atomically.
Map
.Parameters | |
---|---|
key |
K : key with which the specified value is associated |
value |
V : value to be associated with the specified key |
Returns | |
---|---|
V |
the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.) |
Throws | |
---|---|
UnsupportedOperationException |
if the put operation is not supported by this map |
ClassCastException |
if the class of the specified key or value prevents it from being stored in this map |
NullPointerException |
if the specified key or value is null, and this map does not permit null keys or values |
IllegalArgumentException |
if some property of the specified key or value prevents it from being stored in this map |
void replaceAll (BiFunction<? super K, ? super V, ? extends V> function)
用对该条目调用给定函数的结果替换每个条目的值,直到处理完所有条目或者该函数抛出异常。 函数抛出的异常会传递给调用者。
默认实现相当于,对于这个 map
:
for (Map.Entry<K,V> entry : map.entrySet()) {
K k;
V v;
do {
k = entry.getKey();
v = entry.getValue();
} while (!map.replace(k, v, function.apply(k, v)));
}
The default implementation may retry these steps when multiple threads attempt updates including potentially calling the function repeatedly for a given key. 此实现假定ConcurrentMap不能包含空值, get()
返回null的值明确表示该键不存在。 支持空值的实现必须覆盖此默认实现。
Parameters | |
---|---|
function |
BiFunction : the function to apply to each entry |
Throws | |
---|---|
UnsupportedOperationException |
|
NullPointerException |
|
ClassCastException |
|
IllegalArgumentException |