Interface LocalMap<K,V>
-
- All Superinterfaces:
Map<K,V>
public interface LocalMap<K,V> extends Map<K,V>
Local maps can be used to share data safely in a single Vert.x instance.By default the map allows immutable keys and values. Custom keys and values should implement
Shareable
interface. The map returns their copies.This ensures there is no shared access to mutable state from different threads (e.g. different event loops) in the Vert.x instance, and means you don't have to protect access to that state using synchronization or locks.
Since the version 3.4, this class extends the
Map
interface. However some methods are only accessible in Java.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
clear()
Clear all entries in the mapvoid
close()
Close and release the mapV
compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping).V
computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped tonull
), attempts to compute its value using the given mapping function and enters it into this map unlessnull
.V
computeIfPresent(K key, java.util.function.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.boolean
containsKey(Object key)
Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue(Object value)
Returns @{code true} if this map maps one or more keys to the specified value.Set<Map.Entry<K,V>>
entrySet()
Returns aSet
view of the mappings contained in this map.void
forEach(java.util.function.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.V
get(Object key)
Get a value from the mapV
getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.boolean
isEmpty()
Set<K>
keySet()
V
merge(K key, V value, java.util.function.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
put(K key, V value)
Put an entry in the mapvoid
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.V
putIfAbsent(K key, V value)
Put the entry only if there is no existing entry for that keyV
remove(Object key)
Remove an entry from the mapboolean
remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.boolean
removeIfPresent(K key, V value)
Remove the entry only if there is an entry with the specified key and value.V
replace(K key, V value)
Replace the entry only if there is an existing entry with the keyboolean
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.void
replaceAll(java.util.function.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.boolean
replaceIfPresent(K key, V oldValue, V newValue)
Replace the entry only if there is an existing entry with the specified key and value.int
size()
Get the size of the mapCollection<V>
values()
-
-
-
Method Detail
-
size
int size()
Get the size of the map
-
isEmpty
boolean isEmpty()
-
putIfAbsent
V putIfAbsent(K key, V value)
Put the entry only if there is no existing entry for that key- Specified by:
putIfAbsent
in interfaceMap<K,V>
- Parameters:
key
- the keyvalue
- the value- Returns:
- the old value or null, if none
-
removeIfPresent
boolean removeIfPresent(K key, V value)
Remove the entry only if there is an entry with the specified key and value.This method is the poyglot version of
remove(Object, Object)
.- Parameters:
key
- the keyvalue
- the value- Returns:
- true if removed
-
replaceIfPresent
boolean replaceIfPresent(K key, V oldValue, V newValue)
Replace the entry only if there is an existing entry with the specified key and value.This method is the polyglot version of
replace(Object, Object, Object)
.- Parameters:
key
- the keyoldValue
- the old valuenewValue
- the new value- Returns:
- true if removed
-
replace
V replace(K key, V value)
Replace the entry only if there is an existing entry with the key
-
close
void close()
Close and release the map
-
values
Collection<V> values()
-
compute
V compute(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping).If the function returns
null
, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
-
computeIfAbsent
V computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped tonull
), attempts to compute its value using the given mapping function and enters it into this map unlessnull
.If the function returns
null
no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded.- Specified by:
computeIfAbsent
in interfaceMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the 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
-
computeIfPresent
V computeIfPresent(K key, java.util.function.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.If the function returns
null
, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.- Specified by:
computeIfPresent
in interfaceMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none
-
containsKey
boolean containsKey(Object key)
Returnstrue
if this map contains a mapping for the specified key.- Specified by:
containsKey
in interfaceMap<K,V>
- Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified key
-
containsValue
boolean containsValue(Object value)
Returns @{code true} if this map maps one or more keys to the specified value.- Specified by:
containsValue
in interfaceMap<K,V>
- Parameters:
value
- value whose presence in this map is to be tested- Returns:
-
entrySet
Set<Map.Entry<K,V>> entrySet()
Returns aSet
view of the mappings contained in this map.Unlike the default
Map
implementation, the set is not backed by the map. So changes made to the map are not reflected in the set. Entries added or remove to the set are not reflected to the map. In addition, the entries are not modifiable (Map.Entry.setValue(Object)
is not supported).
-
forEach
void forEach(java.util.function.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.Exceptions thrown by the action are relayed to the caller.
-
getOrDefault
V getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.- Specified by:
getOrDefault
in interfaceMap<K,V>
- Parameters:
key
- the key whose associated value is to be returneddefaultValue
- the default mapping of the key- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key
-
merge
V merge(K key, V value, java.util.function.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. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result isnull
. This method may be of use when combining multiple mapped values for a key.- Specified by:
merge
in interfaceMap<K,V>
- Parameters:
key
- key with which the resulting value is to be associatedvalue
- 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 keyremappingFunction
- the function to recompute a value if present- Returns:
- the new value associated with the specified key, or null if no value is associated with the key
-
putAll
void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from keyk
to valuev
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
-
remove
boolean remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.
-
replace
boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
-
replaceAll
void replaceAll(java.util.function.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. Exceptions thrown by the function are relayed to the caller.- Specified by:
replaceAll
in interfaceMap<K,V>
- Parameters:
function
- the function to apply to each entry
-
-