K
- the type of keys maintained by this mapV
- the type of mapped values@ThreadSafe public final class ConcurrentLinkedHashMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>, Serializable
ConcurrentHashMap
in that it maintains a
page replacement algorithm that is used to evict an entry when the map has
exceeded its capacity. Unlike the Java Collections Framework, this
map does not have a publicly visible constructor and instances are created
through a ConcurrentLinkedHashMap.Builder
.
An entry is evicted from the map when the weighted capacity exceeds
a threshold determined by a CapacityLimiter
. The default limiter
bounds the map by its maximum weighted capacity. A Weigher
instance determines how many units of capacity that a value consumes. The
default weigher assigns each value a weight of 1 to bound the map by
the total number of key-value pairs. A map that holds collections may choose
to weigh values by the number of elements in the collection and bound the map
by the total number of elements that it contains. A change to a value that
modifies its weight requires that an update operation is performed on the
map.
An EvictionListener
may be supplied for notification when an entry
is evicted from the map. This listener is invoked on a caller's thread and
will not block other threads from operating on the map. An implementation
should be aware that the caller's thread will not expect long execution
times or failures as a side effect of the listener being notified. Execution
safety and a fast turn around time can be achieved by performing the
operation asynchronously, such as by submitting a task to an
ExecutorService
.
The concurrency level determines the number of threads that can concurrently modify the table. Using a significantly higher or lower value than needed can waste space or lead to thread contention, but an estimate within an order of magnitude of the ideal value does not usually have a noticeable impact. Because placement in hash tables is essentially random, the actual concurrency will vary.
This class and its views and iterators implement all of the
optional methods of the Map
and Iterator
interfaces.
Like Hashtable
but unlike HashMap
, this class
does not allow null to be used as a key or value. Unlike
LinkedHashMap
, this class does not provide
predictable iteration order.
Modifier and Type | Class and Description |
---|---|
static class |
ConcurrentLinkedHashMap.Builder<K,V>
A builder that creates
ConcurrentLinkedHashMap instances. |
AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Method and Description |
---|---|
int |
capacity()
Retrieves the maximum weighted capacity of the map.
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet() |
void |
evictWith(CapacityLimiter capacityLimiter)
Evicts entries from the map while it exceeds the capacity limiter's
constraint or until the map is empty.
|
V |
get(Object key) |
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value) |
V |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
void |
setCapacity(int capacity)
Sets the maximum weighted capacity of the map and eagerly evicts entries
until it shrinks to the appropriate size.
|
int |
size() |
Collection<V> |
values() |
int |
weightedSize()
Returns the weighted size of this map.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
public int capacity()
public void setCapacity(int capacity)
capacity
- the maximum weighted capacity of the mapIllegalArgumentException
- if the capacity is negativepublic void evictWith(CapacityLimiter capacityLimiter)
capacityLimiter
- the algorithm to determine whether to evict an entryNullPointerException
- if the capacity limiter is nullpublic boolean isEmpty()
public int size()
public int weightedSize()
public void clear()
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
putIfAbsent
in interface Map<K,V>
public Collection<V> values()
Copyright © 2019. All rights reserved.