Interface ReplicatedMap<K,V>
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
- All Superinterfaces:
DistributedObject
,Map<K,
V>
A ReplicatedMap is a map data structure with weak consistency and has entries stored locally on every node of the cluster.
Whenever a value is written, the new value will be internally distributed to all existing cluster members, and eventually every node will have the new value. Reading is always local on members (except for lite members).
When a new node joins the cluster, it initially requests existing values from older nodes and replicates them locally.
Supports split-brain protection SplitBrainProtectionConfig
since 3.10
in cluster versions 3.10 and higher.
- Since:
- 3.2
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddEntryListener
(EntryListener<K, V> listener) Adds an entry listener for this map.addEntryListener
(EntryListener<K, V> listener, Predicate<K, V> predicate) Adds an continuous entry listener for this map.addEntryListener
(EntryListener<K, V> listener, Predicate<K, V> predicate, K key) Adds an continuous entry listener for this map.addEntryListener
(EntryListener<K, V> listener, K key) Adds the specified entry listener for the specified key.void
clear()
The clear operation wipes data out of the replicated maps.entrySet()
Returns a lazySet
view of the mappings contained in this map.
A LazySet is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations.Returns LocalReplicatedMapStats for this replicated map.keySet()
Returns a lazySet
view of the key contained in this map.
A LazySet is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations.Associates a given value to the specified key and replicates it to the cluster.boolean
Removes the specified entry listener.values()
Returns a lazyCollection
view of the values contained in this map.
A LazyCollection is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations.values
(Comparator<V> comparator) Returns an eagerly populatedCollection
view of the values contained in this map.Methods inherited from interface com.hazelcast.core.DistributedObject
destroy, getDestroyContextForTenant, getName, getPartitionKey, getServiceName
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, equals, forEach, get, getOrDefault, hashCode, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size
-
Method Details
-
put
Associates a given value to the specified key and replicates it to the cluster. If there is an old value, it will be replaced by the specified one and returned from the call.
In addition, you have to specify a ttl and its
TimeUnit
to define when the value is outdated and thus should be removed from the replicated map.- Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.ttl
- ttl to be associated with the specified key-value pair.timeUnit
- TimeUnit to be used for the ttl value.
-
clear
void clear()The clear operation wipes data out of the replicated maps.
If some node fails on executing the operation, it is retried for at most 5 times (on the failing nodes only).
-
removeEntryListener
Removes the specified entry listener. Returns silently if there was no such listener added before.- Parameters:
id
- ID of the registered entry listener.- Returns:
- true if registration is removed, false otherwise.
-
addEntryListener
Adds an entry listener for this map. The listener will be notified for all map add/remove/update/evict events.- Parameters:
listener
- entry listener
-
addEntryListener
Adds the specified entry listener for the specified key. The listener will be notified for all add/remove/update/evict events of the specified key only.Warning: This method uses
hashCode
andequals
of the binary form of thekey
, not the actual implementations ofhashCode
andequals
defined in thekey
's class.- Parameters:
listener
- the entry listener to addkey
- the key to listen to- Throws:
NullPointerException
- if the specified key is null
-
addEntryListener
@Nonnull UUID addEntryListener(@Nonnull EntryListener<K, V> listener, @Nonnull Predicate<K, V> predicate) Adds an continuous entry listener for this map. The listener will be notified for map add/remove/update/evict events filtered by the given predicate.- Parameters:
listener
- the entry listener to addpredicate
- the predicate for filtering entries
-
addEntryListener
@Nonnull UUID addEntryListener(@Nonnull EntryListener<K, V> listener, @Nonnull Predicate<K, V> predicate, @Nullable K key) Adds an continuous entry listener for this map. The listener will be notified for map add/remove/update/evict events filtered by the given predicate.- Parameters:
listener
- the entry listenerpredicate
- the predicate for filtering entrieskey
- the key to listen to
-
values
Returns a lazyCollection
view of the values contained in this map.
A LazyCollection is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations. Any kind of mutating function will throw anUnsupportedOperationException
. Same is true for operations likeCollection.contains(Object)
andCollection.containsAll(java.util.Collection)
, which would result in very poor performance if called repeatedly (for example, in a loop). If the use case is different from querying the data, please copy the resulting set into a newList
or similar data structure.ReplicatedMap<K, V> repMap = ...; // Returns a LazyCollection Collection<V> values = repMap.values(); List<V> copy = new ArrayList<V>(values); if (copy.containsAll(possibleValues)) { // ... }
Due to the lazy nature of the returned set, changes to the map (addition, removal, update) might be reflected on the collection.
Changes on the map are NOT reflected on the collection on the CLIENT or vice versa. The order of the elements is not guaranteed due to the internal asynchronous replication behavior. If a specific order is needed, usevalues(java.util.Comparator)
to force reordering of the elements before returning.
Changes to any returned object are NOT replicated back to other members. -
values
Returns an eagerly populatedCollection
view of the values contained in this map. The collection is NOT backed by the map, so changes to the map are NOT reflected on the collection, and vice-versa.
The order of the elements is guaranteed by executing the givenComparator
before returning the elements.
Changes to any returned object are NOT replicated back to other members.- Parameters:
comparator
- the Comparator to sort the returned elements.- Returns:
- An eagerly populated
Collection
view of the values contained in this map.
-
entrySet
Returns a lazySet
view of the mappings contained in this map.
A LazySet is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations. Any kind of mutating function will throw anUnsupportedOperationException
. Same is true for operations likeSet.contains(Object)
andSet.containsAll(java.util.Collection)
which would result in very poor performance if called repeatedly (for example, in a loop). If the use case is different from querying the data, please copy the resulting set into a newHashSet
.ReplicatedMap<K, V> repMap = ...; // Returns a LazySet Set<Map.Entry<K, V>> entrySet = repMap.entrySet(); Set<Map.Entry<K, V>> copy = new HashSet<Map.Entry<K, V>>(entrySet);
Due to the lazy nature of the returned set, changes to the map (addition, removal, update) might be reflected on the set.
Changes on the map are NOT reflected on the set on the CLIENT or vice versa. The order of the elements is not guaranteed due to the internal asynchronous replication behavior.
Changes to any returned object are NOT replicated back to other members. -
keySet
Returns a lazySet
view of the key contained in this map.
A LazySet is optimized for querying speed (preventing eager deserialization and hashing on HashSet insertion) and does NOT provide all operations. Any kind of mutating function will throw anUnsupportedOperationException
. Same is true for operations likeSet.contains(Object)
andSet.containsAll(java.util.Collection)
which would result in very poor performance if called repeatedly (for example, in a loop). If the use case is different from querying the data, please copy the resulting set into a newHashSet
.ReplicatedMap<K, V> repMap = ...; // Returns a LazySet Set<K> keySet = repMap.keySet(); Set<K> copy = new HashSet<K>(keySet); for (K key : possibleKeys) { if (!copy.contains(key)) return false; }
Due to the lazy nature of the returned set, changes to the map (addition, removal, update) might be reflected on the set.
Changes on the map are NOT reflected on the set on the CLIENT or vice versa. The order of the elements is not guaranteed due to the internal asynchronous replication behavior.
Changes to any returned object are NOT replicated back to other members. -
getReplicatedMapStats
Returns LocalReplicatedMapStats for this replicated map. LocalReplicatedMapStats is the statistics for the local replicated map and contains information such as getCount putCount, hits, lastUpdateTime etc.- Returns:
- this replicated map's statistics.
-