Interface

interface ConcurrentMap<K, V>

public interface ConcurrentMap<K, V> {
    func add(key: K, value: V): ?V
    func addIfAbsent(key: K, value: V): ?V
    func entryView(key: K, fn: (MapEntryView<K, V>) -> Unit): ?V
    func get(key: K): ?V
    func contains(key: K): Bool
    func put(key: K, value: V): ?V
    func putIfAbsent(key: K, value: V): ?V
    func remove(key: K): ?V
    func remove(key: K, predicate: (V) -> Bool): ?V
    func replace(key: K, value: V): ?V
    func replace(key: K, eval: (V) -> V): ?V
    func replace(key: K, predicate: (V) -> Bool, eval: (V) -> V): ?V
    operator func [](key: K): V
    operator func [](key: K, value!: V): Unit
}

Description: Specifies a definition of the Map interface that ensures thread security and operation atomicity.

The ConcurrentMap interface declares thread-safe methods of Map in concurrency scenarios, and the methods ensures atomicity. All defined thread-safe Map classes should implement the ConcurrentMap interface. For example, ConcurrentHashMap defined in this package implements the ConcurrentMap interface and implements the methods declared in ConcurrentMap. These methods ensure atomicity.

The ConcurrentMap interface declares methods of concurrent Map that needs to ensure atomicity in concurrency scenarios.

The concurrent Map indicates mappings from keys to values, where K indicates a key, and V indicates a value.

func add(K, V)

func add(key: K, value: V): ?V

Description: Associates a specified value with a key specified in a Map instance. If the Map instance already contains the association of the key, the old value is replaced. If the Map instance does not contain the association of the key, the association between the key and the value is added.

Parameters:

  • key: K: key to be put
  • value: V: value to be associated

Returns:

  • ?V: If the key exists before value assignment, the old value Some(V) is returned. If the key does not exist before value assignment, None is returned.

func addIfAbsent(K, V)

func addIfAbsent(key: K, value: V): ?V

Description: Adds the association between a specified value and a specified key to a Map instance if the Map instance does not contain the key. If the Map instance already contains the key, the value assignment is not performed.

Parameters:

  • key: K: key to be put
  • value: V: value to be assigned

Returns:

  • ?V: If the key exists before value assignment, the value Some(V) corresponding to the current key is returned and the value assignment is not performed. If the key does not exist before value assignment, None is returned.

func contains(K)

func contains(key: K): Bool

Description: Checks whether a Map instance contains the association of a specified key.

Parameters:

  • key: K: key to be checked

Returns:

  • Bool: If the key exists, true is returned. If the key does not exist, false is returned.

func entryView(K, (MapEntryView<K, V>) -> Unit)

func entryView(key: K, fn: (MapEntryView<K, V>) -> Unit): ?V

Description: Obtains the entryView of the corresponding key-value pair in the current mapping based on a specified key, calls the fn function to add, delete, or modify the key-value pair, and returns the value corresponding to the key in the final mapping.

If the current mapping does not contain the key, an empty entryView is obtained. In this case, if the value is set to a value other than None, the key-value pair is added to the current mapping.

If the current mapping contains the key, the key-value view is obtained. In this case, if the value is set to None, the key-value pair is deleted from the current mapping; if the value is set to a new value other than None, the value of the key in the current mapping is changed.

Parameters:

  • key: K: key whose view is to be obtained
  • fn: (MapEntryView<K, V>) -> Unit: custom operation on the specified view, which can be used to add, delete, or modify key-value pairs in the mapping

Returns:

  • ?V: value of key in the current mapping after the fn function is called. If the key does not exist, None is returned.

func get(K)

func get(key: K): ?V

Description: Returns the value associated with a key in a Map instance.

Parameters:

  • key: K: key used to obtain a value

Returns:

  • ?V: If the key exists, the value Some (V) associated with the key is returned. If the key does not exist, None is returned.

func put(K, V) (deprecated)

func put(key: K, value: V): ?V

Description: Associates a specified value with a key specified in a Map instance. If the Map instance already contains the association of the key, the old value is replaced. If the Map instance does not contain the association of the key, the association between the key and the value is added.

NOTE

This function will be deprecated in future releases and add(K, V) will be used instead.

Parameters:

  • key: K: key to be put
  • value: V: value to be associated

Returns:

  • ?V: If the key exists before value assignment, the old value Some(V) is returned. If the key does not exist before value assignment, None is returned.

func putIfAbsent(K, V) (deprecated)

func putIfAbsent(key: K, value: V): ?V

Description: Adds the association between a specified value and a specified key to a Map instance if the Map instance does not contain the key. If the Map instance already contains the key, the value assignment is not performed.

NOTE

This function will be deprecated in future releases and addIfAbsent(K, V) will be used instead.

Parameters:

  • key: K: key to be put
  • value: V: value to be assigned

Returns:

  • ?V: If the key exists before value assignment, the value Some(V) corresponding to the current key is returned and the value assignment is not performed. If the key does not exist before value assignment, None is returned.

func remove(K)

func remove(key: K): ?V

Description: Deletes the mapping (if any) of a specified key from a mapping.

Parameters:

  • key: K: key to be deleted

Returns:

  • ?V: If the key exists before the deletion, the value Some (V) corresponding to the key is returned. If the key does not exist before the deletion, None is returned.

func remove(K, (V) -> Bool) (deprecated)

func remove(key: K, predicate: (V) -> Bool): ?V

Description: Deletes the association of a key from a Map instance if the Map instance contains the key and the value v associated with the key meets the condition of predicate.

NOTE

This function will be deprecated in future releases and entryView(K, (MapEntryView<K, V>) -> Unit) will be used instead.

Parameters:

  • key: K: key to be deleted
  • predicate: (V) ->Bool: lambda expression used for judgment

Returns:

  • ?V: If the Map instance contains the key, the old value Some(V) corresponding to key is returned. If the Map instance does not contain the key or the value associated with the key does not meet the condition of predicate, None is returned.

func replace(K, (V) -> Bool, (V) -> V) (deprecated)

func replace(key: K, predicate: (V) -> Bool, eval: (V) -> V): ?V

Description: Replaces the value associated with a key in a Map instance with the calculation result of eval(v) if the key (assuming that the value associated with the key is v) exists in the Map instance and v meets the condition of predicate. The Map instance is not modified if the key does not exist in the Map instance or the key exists but the value associated with the key does not meet the condition of predicate.

NOTE

This function will be deprecated in future releases and entryView(K, (MapEntryView<K, V>) -> Unit) will be used instead.

Parameters:

  • key: K: key whose associated value is to be replaced
  • predicate: (V) ->Bool: lambda expression used for judgment
  • eval: (V) ->V: function used to calculate a new value used for replacement

Returns:

  • ?V: If the key exists, the old value Some(V) corresponding to the key is returned. If the key does not exist or the value associated with the key does not meet the condition of predicate, None is returned.

func replace(K, (V) -> V) (deprecated)

func replace(key: K, eval: (V) -> V): ?V

Description: Replaces the value associated with a key in a Map instance with the calculation result of eval(v) if the key (assuming that the value associated with the key is v) exists in the Map instance. The Map instance is not modified if the key does not exist in the Map instance.

NOTE

This function will be deprecated in future releases and entryView(K, (MapEntryView<K, V>) -> Unit) will be used instead.

Parameters:

  • key: K: key whose associated value is to be replaced
  • eval: (V) ->V: function used to calculate a new value used for replacement

Returns:

  • ?V: If the key exists, the old value Some(V) corresponding to the key is returned. If the key does not exist, None is returned.

func replace(K, V)

func replace(key: K, value: V): ?V

Description: Replaces the value associated with a key in a Map instance with the value specified by value if the key exists in the Map instance. The Map instance is not modified if the key does not exist in the Map instance.

Parameters:

  • key: K: key whose associated value is to be replaced
  • value: V: new value used for replacement

Returns:

  • ?V: If the key exists, the old value Some(V) corresponding to the key is returned. If the key does not exist, None is returned.

operator func [](K)

operator func [](key: K): V

Description: Obtains the value of a specified key. If the key exists, the corresponding value is returned. If the key does not exist, an exception is thrown.

Parameters:

  • key: K: key whose value is to be obtained

Returns:

  • V: value corresponding to the key

Throws:

  • NoneValueException: If the key does not exist in the current mapping, this exception is thrown.

operator func [](K, V)

operator func [](key: K, value!: V): Unit

Description: Maps a value to a specified key. If the key exists, the new value overwrites the old value. If the key does not exist, the key-value pair is added.

Parameters:

  • key: K: key whose value is to be mapped
  • value!: V: value used for mapping