Class ConcurrentLinkedHashMap<K,V>
- Type Parameters:
- K- the type of keys maintained by this map
- V- the type of mapped values
- All Implemented Interfaces:
- Serializable,- ConcurrentMap<K,V>,- Map<K,V>
public 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 its
 maximum weighted capacity threshold. 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. A snapshot of the keys and entries may be obtained in ascending and
 descending order of retention.
- See Also:
- Serialized Form
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classConcurrentLinkedHashMap.Builder<K,V>A builder that createsConcurrentLinkedHashMapinstances.Nested classes/interfaces inherited from class java.util.AbstractMapAbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
- 
Method SummaryModifier and Type Method Description Set<K>ascendingKeySet()Returns a unmodifiable snapshotSetview of the keys contained in this map.Set<K>ascendingKeySetWithLimit(int limit)Returns an unmodifiable snapshotSetview of the keys contained in this map.Map<K,V>ascendingMap()Returns an unmodifiable snapshotMapview of the mappings contained in this map.Map<K,V>ascendingMapWithLimit(int limit)Returns an unmodifiable snapshotMapview of the mappings contained in this map.intcapacity()Retrieves the maximum weighted capacity of the map.voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)Set<K>descendingKeySet()Returns an unmodifiable snapshotSetview of the keys contained in this map.Set<K>descendingKeySetWithLimit(int limit)Returns an unmodifiable snapshotSetview of the keys contained in this map.Map<K,V>descendingMap()Returns an unmodifiable snapshotMapview of the mappings contained in this map.Map<K,V>descendingMapWithLimit(int limit)Returns an unmodifiable snapshotMapview of the mappings contained in this map.Set<Map.Entry<K,V>>entrySet()Vget(Object key)booleanisEmpty()Set<K>keySet()Vput(K key, V value)VputIfAbsent(K key, V value)Vremove(Object key)booleanremove(Object key, Object value)Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)voidsetCapacity(int capacity)Sets the maximum weighted capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.intsize()Collection<V>values()intweightedSize()Returns the weighted size of this map.Methods inherited from class java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.ConcurrentMapcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
- 
Method Details- 
capacitypublic int capacity()Retrieves the maximum weighted capacity of the map.- Returns:
- the maximum weighted capacity
 
- 
setCapacitypublic void setCapacity(int capacity)Sets the maximum weighted capacity of the map and eagerly evicts entries until it shrinks to the appropriate size.- Parameters:
- capacity- the maximum weighted capacity of the map
- Throws:
- IllegalArgumentException- if the capacity is negative
 
- 
isEmptypublic boolean isEmpty()
- 
sizepublic int size()
- 
weightedSizepublic int weightedSize()Returns the weighted size of this map.- Returns:
- the combined weight of the values in this map
 
- 
clearpublic void clear()
- 
containsKey- Specified by:
- containsKeyin interface- Map<K,V>
- Overrides:
- containsKeyin class- AbstractMap<K,V>
 
- 
containsValue- Specified by:
- containsValuein interface- Map<K,V>
- Overrides:
- containsValuein class- AbstractMap<K,V>
 
- 
get
- 
put
- 
putIfAbsent- Specified by:
- putIfAbsentin interface- ConcurrentMap<K,V>
- Specified by:
- putIfAbsentin interface- Map<K,V>
 
- 
remove
- 
remove
- 
replace
- 
replace
- 
keySet
- 
ascendingKeySetReturns a unmodifiable snapshotSetview of the keys contained in this map. The set's iterator returns the keys whose order of iteration is the ascending order in which its entries are considered eligible for retention, from the least-likely to be retained to the most-likely.Beware that, unlike in keySet(), obtaining the set is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the keys.- Returns:
- an ascending snapshot view of the keys in this map
 
- 
ascendingKeySetWithLimitReturns an unmodifiable snapshotSetview of the keys contained in this map. The set's iterator returns the keys whose order of iteration is the ascending order in which its entries are considered eligible for retention, from the least-likely to be retained to the most-likely.Beware that, unlike in keySet(), obtaining the set is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the keys.- Parameters:
- limit- the maximum size of the returned set
- Returns:
- a ascending snapshot view of the keys in this map
- Throws:
- IllegalArgumentException- if the limit is negative
 
- 
descendingKeySetReturns an unmodifiable snapshotSetview of the keys contained in this map. The set's iterator returns the keys whose order of iteration is the descending order in which its entries are considered eligible for retention, from the most-likely to be retained to the least-likely.Beware that, unlike in keySet(), obtaining the set is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the keys.- Returns:
- a descending snapshot view of the keys in this map
 
- 
descendingKeySetWithLimitReturns an unmodifiable snapshotSetview of the keys contained in this map. The set's iterator returns the keys whose order of iteration is the descending order in which its entries are considered eligible for retention, from the most-likely to be retained to the least-likely.Beware that, unlike in keySet(), obtaining the set is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the keys.- Parameters:
- limit- the maximum size of the returned set
- Returns:
- a descending snapshot view of the keys in this map
- Throws:
- IllegalArgumentException- if the limit is negative
 
- 
values
- 
entrySet
- 
ascendingMapReturns an unmodifiable snapshotMapview of the mappings contained in this map. The map's collections return the mappings whose order of iteration is the ascending order in which its entries are considered eligible for retention, from the least-likely to be retained to the most-likely.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries. - Returns:
- a ascending snapshot view of this map
 
- 
ascendingMapWithLimitReturns an unmodifiable snapshotMapview of the mappings contained in this map. The map's collections return the mappings whose order of iteration is the ascending order in which its entries are considered eligible for retention, from the least-likely to be retained to the most-likely.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries. - Parameters:
- limit- the maximum size of the returned map
- Returns:
- a ascending snapshot view of this map
- Throws:
- IllegalArgumentException- if the limit is negative
 
- 
descendingMapReturns an unmodifiable snapshotMapview of the mappings contained in this map. The map's collections return the mappings whose order of iteration is the descending order in which its entries are considered eligible for retention, from the most-likely to be retained to the least-likely.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries. - Returns:
- a descending snapshot view of this map
 
- 
descendingMapWithLimitReturns an unmodifiable snapshotMapview of the mappings contained in this map. The map's collections return the mappings whose order of iteration is the descending order in which its entries are considered eligible for retention, from the most-likely to be retained to the least-likely.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries. - Parameters:
- limit- the maximum size of the returned map
- Returns:
- a descending snapshot view of this map
- Throws:
- IllegalArgumentException- if the limit is negative
 
 
-