public class ReferenceMap<K,V> extends Object implements Map<K,V>, Serializable
The concurrent semantics of ConcurrentHashMap
combined with the
fact that the garbage collector can asynchronously reclaim and clean up
after keys and values at any time can lead to some racy semantics. For
example, size()
returns an upper bound on the size, i.e. the actual
size may be smaller in cases where the key or value has been reclaimed but
the map entry has not been cleaned up yet.
Another example: If get(Object)
cannot find an existing entry
for a key, it will try to create one. This operation is not atomic. One
thread could put(Object, Object)
a value between the time another
thread running get()
checks for an entry and decides to create one.
In this case, the newly created value will replace the put value in the
map. Also, two threads running get()
concurrently can potentially
create duplicate values for a given key.
In other words, this class is great for caching but not atomicity.
Modifier and Type | Class and Description |
---|---|
protected static interface |
ReferenceMap.Strategy |
Constructor and Description |
---|
ReferenceMap(ReferenceType keyReferenceType,
ReferenceType valueReferenceType)
Concurrent hash map that wraps keys and/or values based on specified
reference types.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet()
Returns an unmodifiable set view of the entries in this map.
|
V |
get(Object key) |
protected com.opensymphony.xwork2.inject.util.ReferenceMap.PutStrategy |
getPutStrategy() |
boolean |
isEmpty() |
Set<K> |
keySet()
Returns an unmodifiable set view of the keys in this map.
|
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> t) |
V |
putIfAbsent(K key,
V value) |
protected ReferenceMap.Strategy |
putIfAbsentStrategy() |
protected ReferenceMap.Strategy |
putStrategy() |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
protected ReferenceMap.Strategy |
replaceStrategy() |
int |
size() |
Collection<V> |
values()
Returns an unmodifiable set view of the values in this map.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, replaceAll
public ReferenceMap(ReferenceType keyReferenceType, ReferenceType valueReferenceType)
keyReferenceType
- key reference typevalueReferenceType
- value reference typepublic boolean containsKey(Object key)
containsKey
in interface Map<K,V>
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
public Set<K> keySet()
public Collection<V> values()
public Set<Map.Entry<K,V>> entrySet()
protected ReferenceMap.Strategy putStrategy()
protected ReferenceMap.Strategy putIfAbsentStrategy()
protected ReferenceMap.Strategy replaceStrategy()
protected com.opensymphony.xwork2.inject.util.ReferenceMap.PutStrategy getPutStrategy()
Copyright © 2000–2020 Apache Software Foundation. All rights reserved.