Sprache

IConcurrentMap.IMap.ComputeIfAbsent(Object, IFunction) Method

Definition

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.

[Android.Runtime.Register("computeIfAbsent", "(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;", "GetComputeIfAbsent_Ljava_lang_Object_Ljava_util_function_Function_Handler:Java.Util.Concurrent.IConcurrentMap, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", ApiSince=24)]
override sealed Java.Lang.Object? IMap.ComputeIfAbsent(Java.Lang.Object? key, Java.Util.Functions.IFunction? mappingFunction);
[<Android.Runtime.Register("computeIfAbsent", "(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;", "GetComputeIfAbsent_Ljava_lang_Object_Ljava_util_function_Function_Handler:Java.Util.Concurrent.IConcurrentMap, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", ApiSince=24)>]
abstract member Java.Util.IMap.ComputeIfAbsent : Java.Lang.Object * Java.Util.Functions.IFunction -> Java.Lang.Object
override this.Java.Util.IMap.ComputeIfAbsent : Java.Lang.Object * Java.Util.Functions.IFunction -> Java.Lang.Object

Parameters

key
Object

key with which the specified value is to be associated

mappingFunction
IFunction

the mapping function to compute a value

Returns

the current (existing or computed) value associated with the specified key, or null if the computed value is null

Implements

Attributes

Remarks

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. If the mapping function returns null, no mapping is recorded. If the mapping function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.

Added in 1.8.

The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:

map.computeIfAbsent(key, k -> new Value(f(k)));

Or to implement a multi-value map, Map<K,Collection<V>>, supporting multiple values per key:

map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);

The mapping function should not modify this map during computation.

Java reference for java.util.concurrent.ConcurrentMap.computeIfAbsent.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to