K
- the type of keys maintained by this mapV
- the type of mapped valuesclass ConcurrentHashMapV8<K,V>
extends java.util.AbstractMap<K,V>
implements java.util.concurrent.ConcurrentMap<K,V>, java.io.Serializable
Hashtable
, and
includes versions of methods corresponding to each method of
Hashtable
. However, even though all operations are
thread-safe, retrieval operations do not entail locking,
and there is not any support for locking the entire table
in a way that prevents all access. This class is fully
interoperable with Hashtable
in programs that rely on its
thread safety but not on its synchronization details.
Retrieval operations (including get
) generally do not
block, so may overlap with update operations (including put
and remove
). Retrievals reflect the results of the most
recently completed update operations holding upon their
onset. (More formally, an update operation for a given key bears a
happens-before relation with any (non-null) retrieval for
that key reporting the updated value.) For aggregate operations
such as putAll
and clear
, concurrent retrievals may
reflect insertion or removal of only some entries. Similarly,
Iterators and Enumerations return elements reflecting the state of
the hash table at some point at or since the creation of the
iterator/enumeration. They do not throw ConcurrentModificationException
. However, iterators are designed
to be used by only one thread at a time. Bear in mind that the
results of aggregate status methods including size
, isEmpty
, and containsValue
are typically useful only when
a map is not undergoing concurrent updates in other threads.
Otherwise the results of these methods reflect transient states
that may be adequate for monitoring or estimation purposes, but not
for program control.
The table is dynamically expanded when there are too many
collisions (i.e., keys that have distinct hash codes but fall into
the same slot modulo the table size), with the expected average
effect of maintaining roughly two bins per mapping (corresponding
to a 0.75 load factor threshold for resizing). There may be much
variance around this average as mappings are added and removed, but
overall, this maintains a commonly accepted time/space tradeoff for
hash tables. However, resizing this or any other kind of hash
table may be a relatively slow operation. When possible, it is a
good idea to provide a size estimate as an optional initialCapacity
constructor argument. An additional optional
loadFactor
constructor argument provides a further means of
customizing initial table capacity by specifying the table density
to be used in calculating the amount of space to allocate for the
given number of elements. Also, for compatibility with previous
versions of this class, constructors may optionally specify an
expected concurrencyLevel
as an additional hint for
internal sizing. Note that using many keys with exactly the same
hashCode()
is a sure way to slow down performance of any
hash table. To ameliorate impact, when keys are Comparable
,
this class may use comparison order among keys to help break ties.
A Set
projection of a ConcurrentHashMapV8 may be created
(using newKeySet()
or newKeySet(int)
), or viewed
(using keySet(Object)
when only keys are of interest, and the
mapped values are (perhaps transiently) not used or all take the
same mapping value.
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.
ConcurrentHashMapV8s support a set of sequential and parallel bulk
operations that are designed
to be safely, and often sensibly, applied even with maps that are
being concurrently updated by other threads; for example, when
computing a snapshot summary of the values in a shared registry.
There are three kinds of operation, each with four forms, accepting
functions with Keys, Values, Entries, and (Key, Value) arguments
and/or return values. Because the elements of a ConcurrentHashMapV8
are not ordered in any particular way, and may be processed in
different orders in different parallel executions, the correctness
of supplied functions should not depend on any ordering, or on any
other objects or values that may transiently change while
computation is in progress; and except for forEach actions, should
ideally be side-effect-free. Bulk operations on Map.Entry
objects do not support method setValue
.
These bulk operations accept a parallelismThreshold
argument. Methods proceed sequentially if the current map size is
estimated to be less than the given threshold. Using a value of
Long.MAX_VALUE
suppresses all parallelism. Using a value
of 1
results in maximal parallelism by partitioning into
enough subtasks to fully utilize the ForkJoinPool#commonPool()
that is used for all parallel
computations. Normally, you would initially choose one of these
extreme values, and then measure performance of using in-between
values that trade off overhead versus throughput.
The concurrency properties of bulk operations follow
from those of ConcurrentHashMapV8: Any non-null result returned
from get(key)
and related access methods bears a
happens-before relation with the associated insertion or
update. The result of any bulk operation reflects the
composition of these per-element relations (but is not
necessarily atomic with respect to the map as a whole unless it
is somehow known to be quiescent). Conversely, because keys
and values in the map are never null, null serves as a reliable
atomic indicator of the current lack of any result. To
maintain this property, null serves as an implicit basis for
all non-scalar reduction operations. For the double, long, and
int versions, the basis should be one that, when combined with
any other value, returns that other value (more formally, it
should be the identity element for the reduction). Most common
reductions have these properties; for example, computing a sum
with basis 0 or a minimum with basis MAX_VALUE.
Search and transformation functions provided as arguments should similarly return null to indicate the lack of any result (in which case it is not used). In the case of mapped reductions, this also enables transformations to serve as filters, returning null (or, in the case of primitive specializations, the identity basis) if the element should not be combined. You can create compound transformations and filterings by composing them yourself under this "null means there is nothing there now" rule before using them in search or reduce operations.
Methods accepting and/or returning Entry arguments maintain
key-value associations. They may be useful for example when
finding the key for the greatest value. Note that "plain" Entry
arguments can be supplied using new
AbstractMap.SimpleEntry(k,v)
.
Bulk operations may complete abruptly, throwing an exception encountered in the application of a supplied function. Bear in mind when handling such exceptions that other concurrently executing functions could also have thrown exceptions, or would have done so if the first exception had not occurred.
Speedups for parallel compared to sequential forms are common but not guaranteed. Parallel operations involving brief functions on small maps may execute more slowly than sequential forms if the underlying work to parallelize the computation is more expensive than the computation itself. Similarly, parallelization may not lead to much actual parallelism if all processors are busy performing unrelated tasks.
All arguments to all task methods must be non-null.
jsr166e note: During transition, this class uses nested functional interfaces with different names but the same forms as those expected for JDK8.
This class is a member of the Java Collections Framework.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
ConcurrentHashMapV8.BaseIterator<K,V>
Base of key, value, and entry Iterators.
|
(package private) static class |
ConcurrentHashMapV8.CollectionView<K,V,E>
Base class for views.
|
(package private) static class |
ConcurrentHashMapV8.CounterCell |
(package private) static class |
ConcurrentHashMapV8.CounterHashCode
Holder for the thread-local hash code determining which
CounterCell to use.
|
(package private) static class |
ConcurrentHashMapV8.EntryIterator<K,V> |
(package private) static class |
ConcurrentHashMapV8.EntrySetView<K,V>
A view of a ConcurrentHashMapV8 as a
Set of (key, value)
entries. |
(package private) static class |
ConcurrentHashMapV8.ForwardingNode<K,V>
A node inserted at head of bins during transfer operations.
|
(package private) static class |
ConcurrentHashMapV8.KeyIterator<K,V> |
static class |
ConcurrentHashMapV8.KeySetView<K,V>
A view of a ConcurrentHashMapV8 as a
Set of keys, in
which additions may optionally be enabled by mapping to a
common value. |
(package private) static class |
ConcurrentHashMapV8.MapEntry<K,V>
Exported Entry for EntryIterator
|
(package private) static class |
ConcurrentHashMapV8.Node<K,V>
Key-value entry.
|
(package private) static class |
ConcurrentHashMapV8.ReservationNode<K,V>
A place-holder node used in computeIfAbsent and compute
|
(package private) static class |
ConcurrentHashMapV8.Segment<K,V>
Stripped-down version of helper class used in previous version,
declared for the sake of serialization compatibility
|
(package private) static class |
ConcurrentHashMapV8.Traverser<K,V>
Encapsulates traversal for methods such as containsValue; also
serves as a base class for other iterators and spliterators.
|
(package private) static class |
ConcurrentHashMapV8.TreeBin<K,V>
TreeNodes used at the heads of bins.
|
(package private) static class |
ConcurrentHashMapV8.TreeNode<K,V>
Nodes for use in TreeBins
|
(package private) static class |
ConcurrentHashMapV8.ValueIterator<K,V> |
(package private) static class |
ConcurrentHashMapV8.ValuesView<K,V>
A view of a ConcurrentHashMapV8 as a
Collection of
values, in which additions are disabled. |
Modifier and Type | Field and Description |
---|---|
private static long |
ABASE |
private static int |
ASHIFT |
private long |
baseCount
Base counter value, used mainly when there is no contention,
but also as a fallback during table initialization
races.
|
private static long |
BASECOUNT |
private int |
cellsBusy
Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
|
private static long |
CELLSBUSY |
private static long |
CELLVALUE |
private ConcurrentHashMapV8.CounterCell[] |
counterCells
Table of counter cells.
|
(package private) static java.util.concurrent.atomic.AtomicInteger |
counterHashCodeGenerator
Generates initial value for per-thread CounterHashCodes.
|
private static int |
DEFAULT_CAPACITY
The default initial table capacity.
|
private static int |
DEFAULT_CONCURRENCY_LEVEL
The default concurrency level for this table.
|
private ConcurrentHashMapV8.EntrySetView<K,V> |
entrySet |
(package private) static int |
HASH_BITS |
private ConcurrentHashMapV8.KeySetView<K,V> |
keySet |
private static float |
LOAD_FACTOR
The load factor for this table.
|
(package private) static int |
MAX_ARRAY_SIZE
The largest possible (non-power of two) array size.
|
private static int |
MAXIMUM_CAPACITY
The largest possible table capacity.
|
private static int |
MIN_TRANSFER_STRIDE
Minimum number of rebinnings per transfer step.
|
(package private) static int |
MIN_TREEIFY_CAPACITY
The smallest table capacity for which bins may be treeified.
|
(package private) static int |
MOVED |
(package private) static int |
NCPU
Number of CPUS, to place bounds on some sizings
|
private ConcurrentHashMapV8.Node<K,V>[] |
nextTable
The next table to use; non-null only while resizing.
|
(package private) static int |
RESERVED |
(package private) static int |
SEED_INCREMENT
Increment for counterHashCodeGenerator.
|
private static java.io.ObjectStreamField[] |
serialPersistentFields
For serialization compatibility.
|
private static long |
serialVersionUID |
private int |
sizeCtl
Table initialization and resizing control.
|
private static long |
SIZECTL |
(package private) ConcurrentHashMapV8.Node<K,V>[] |
table
The array of bins.
|
(package private) static java.lang.ThreadLocal<ConcurrentHashMapV8.CounterHashCode> |
threadCounterHashCode
Per-thread counter hash codes.
|
private int |
transferIndex
The next table index (plus one) to split while resizing.
|
private static long |
TRANSFERINDEX |
private int |
transferOrigin
The least available table index to split while resizing.
|
private static long |
TRANSFERORIGIN |
(package private) static int |
TREEBIN |
(package private) static int |
TREEIFY_THRESHOLD
The bin count threshold for using a tree rather than list for a
bin.
|
private static sun.misc.Unsafe |
U |
(package private) static int |
UNTREEIFY_THRESHOLD
The bin count threshold for untreeifying a (split) bin during a
resize operation.
|
private ConcurrentHashMapV8.ValuesView<K,V> |
values |
Constructor and Description |
---|
ConcurrentHashMapV8()
Creates a new, empty map with the default initial table size (16).
|
ConcurrentHashMapV8(int initialCapacity)
Creates a new, empty map with an initial table size
accommodating the specified number of elements without the need
to dynamically resize.
|
ConcurrentHashMapV8(int initialCapacity,
float loadFactor)
Creates a new, empty map with an initial table size based on
the given number of elements (
initialCapacity ) and
initial table density (loadFactor ). |
ConcurrentHashMapV8(int initialCapacity,
float loadFactor,
int concurrencyLevel)
Creates a new, empty map with an initial table size based on
the given number of elements (
initialCapacity ), table
density (loadFactor ), and number of concurrently
updating threads (concurrencyLevel ). |
ConcurrentHashMapV8(java.util.Map<? extends K,? extends V> m)
Creates a new map with the same mappings as the given map.
|
Modifier and Type | Method and Description |
---|---|
private void |
addCount(long x,
int check)
Adds to count, and if table is too small and not already
resizing, initiates transfer.
|
(package private) static <K,V> boolean |
casTabAt(ConcurrentHashMapV8.Node<K,V>[] tab,
int i,
ConcurrentHashMapV8.Node<K,V> c,
ConcurrentHashMapV8.Node<K,V> v) |
void |
clear()
Removes all of the mappings from this map.
|
(package private) static java.lang.Class<?> |
comparableClassFor(java.lang.Object x)
Returns x's Class if it is of the form "class C implements
Comparable
|
(package private) static int |
compareComparables(java.lang.Class<?> kc,
java.lang.Object k,
java.lang.Object x)
Returns k.compareTo(x) if x matches kc (k's screened comparable
class), else 0.
|
boolean |
contains(java.lang.Object value)
Deprecated.
|
boolean |
containsKey(java.lang.Object key)
Tests if the specified object is a key in this table.
|
boolean |
containsValue(java.lang.Object value)
Returns
true if this map maps one or more keys to the
specified value. |
java.util.Enumeration<V> |
elements()
Returns an enumeration of the values in this table.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this map for equality.
|
private void |
fullAddCount(long x,
ConcurrentHashMapV8.CounterHashCode hc,
boolean wasUncontended) |
V |
get(java.lang.Object key)
Returns the value to which the specified key is mapped,
or
null if this map contains no mapping for the key. |
V |
getOrDefault(java.lang.Object key,
V defaultValue)
Returns the value to which the specified key is mapped, or the
given default value if this map contains no mapping for the
key.
|
private static sun.misc.Unsafe |
getUnsafe()
Returns a sun.misc.Unsafe.
|
int |
hashCode()
Returns the hash code value for this
Map , i.e.,
the sum of, for each key-value pair in the map,
key.hashCode() ^ value.hashCode() . |
(package private) ConcurrentHashMapV8.Node<K,V>[] |
helpTransfer(ConcurrentHashMapV8.Node<K,V>[] tab,
ConcurrentHashMapV8.Node<K,V> f)
Helps transfer if a resize is in progress.
|
private ConcurrentHashMapV8.Node<K,V>[] |
initTable()
Initializes table, using the size recorded in sizeCtl.
|
boolean |
isEmpty() |
java.util.Enumeration<K> |
keys()
Returns an enumeration of the keys in this table.
|
ConcurrentHashMapV8.KeySetView<K,V> |
keySet()
Returns a
Set view of the keys contained in this map. |
ConcurrentHashMapV8.KeySetView<K,V> |
keySet(V mappedValue)
Returns a
Set view of the keys in this map, using the
given common mapped value for any additions (i.e., Collection.add(E) and Collection.addAll(Collection) ). |
long |
mappingCount()
Returns the number of mappings.
|
static <K> ConcurrentHashMapV8.KeySetView<K,java.lang.Boolean> |
newKeySet()
Creates a new
Set backed by a ConcurrentHashMapV8
from the given type to Boolean.TRUE . |
static <K> ConcurrentHashMapV8.KeySetView<K,java.lang.Boolean> |
newKeySet(int initialCapacity)
Creates a new
Set backed by a ConcurrentHashMapV8
from the given type to Boolean.TRUE . |
V |
put(K key,
V value)
Maps the specified key to the specified value in this table.
|
void |
putAll(java.util.Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this one.
|
V |
putIfAbsent(K key,
V value) |
(package private) V |
putVal(K key,
V value,
boolean onlyIfAbsent)
Implementation for put and putIfAbsent
|
private void |
readObject(java.io.ObjectInputStream s)
Reconstitutes the instance from a stream (that is, deserializes it).
|
V |
remove(java.lang.Object key)
Removes the key (and its corresponding value) from this map.
|
boolean |
remove(java.lang.Object key,
java.lang.Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
(package private) V |
replaceNode(java.lang.Object key,
V value,
java.lang.Object cv)
Implementation for the four public remove/replace methods:
Replaces node value with v, conditional upon match of cv if
non-null.
|
(package private) static <K,V> void |
setTabAt(ConcurrentHashMapV8.Node<K,V>[] tab,
int i,
ConcurrentHashMapV8.Node<K,V> v) |
int |
size() |
(package private) static int |
spread(int h)
Spreads (XORs) higher bits of hash to lower and also forces top
bit to 0.
|
(package private) long |
sumCount() |
(package private) static <K,V> ConcurrentHashMapV8.Node<K,V> |
tabAt(ConcurrentHashMapV8.Node<K,V>[] tab,
int i) |
private static int |
tableSizeFor(int c)
Returns a power of two table size for the given desired capacity.
|
java.lang.String |
toString()
Returns a string representation of this map.
|
private void |
transfer(ConcurrentHashMapV8.Node<K,V>[] tab,
ConcurrentHashMapV8.Node<K,V>[] nextTab)
Moves and/or copies the nodes in each bin to new table.
|
private void |
treeifyBin(ConcurrentHashMapV8.Node<K,V>[] tab,
int index)
Replaces all linked nodes in bin at given index unless table is
too small, in which case resizes instead.
|
private void |
tryPresize(int size)
Tries to presize table to accommodate the given number of elements.
|
(package private) static <K,V> ConcurrentHashMapV8.Node<K,V> |
untreeify(ConcurrentHashMapV8.Node<K,V> b)
Returns a list on non-TreeNodes replacing those in given list.
|
java.util.Collection<V> |
values()
Returns a
Collection view of the values contained in this map. |
private void |
writeObject(java.io.ObjectOutputStream s)
Saves the state of the
ConcurrentHashMapV8 instance to a
stream (i.e., serializes it). |
private static final long serialVersionUID
private static final int MAXIMUM_CAPACITY
private static final int DEFAULT_CAPACITY
static final int MAX_ARRAY_SIZE
private static final int DEFAULT_CONCURRENCY_LEVEL
private static final float LOAD_FACTOR
n - (n >>> 2)
for
the associated resizing threshold.static final int TREEIFY_THRESHOLD
static final int UNTREEIFY_THRESHOLD
static final int MIN_TREEIFY_CAPACITY
private static final int MIN_TRANSFER_STRIDE
static final int MOVED
static final int TREEBIN
static final int RESERVED
static final int HASH_BITS
static final int NCPU
private static final java.io.ObjectStreamField[] serialPersistentFields
transient volatile ConcurrentHashMapV8.Node<K,V>[] table
private transient volatile ConcurrentHashMapV8.Node<K,V>[] nextTable
private transient volatile long baseCount
private transient volatile int sizeCtl
private transient volatile int transferIndex
private transient volatile int transferOrigin
private transient volatile int cellsBusy
private transient volatile ConcurrentHashMapV8.CounterCell[] counterCells
private transient ConcurrentHashMapV8.KeySetView<K,V> keySet
private transient ConcurrentHashMapV8.ValuesView<K,V> values
private transient ConcurrentHashMapV8.EntrySetView<K,V> entrySet
static final java.util.concurrent.atomic.AtomicInteger counterHashCodeGenerator
static final int SEED_INCREMENT
static final java.lang.ThreadLocal<ConcurrentHashMapV8.CounterHashCode> threadCounterHashCode
private static final sun.misc.Unsafe U
private static final long SIZECTL
private static final long TRANSFERINDEX
private static final long TRANSFERORIGIN
private static final long BASECOUNT
private static final long CELLSBUSY
private static final long CELLVALUE
private static final long ABASE
private static final int ASHIFT
ConcurrentHashMapV8()
ConcurrentHashMapV8(int initialCapacity)
initialCapacity
- The implementation performs internal
sizing to accommodate this many elements.java.lang.IllegalArgumentException
- if the initial capacity of
elements is negativeConcurrentHashMapV8(java.util.Map<? extends K,? extends V> m)
m
- the mapConcurrentHashMapV8(int initialCapacity, float loadFactor)
initialCapacity
) and
initial table density (loadFactor
).initialCapacity
- the initial capacity. The implementation
performs internal sizing to accommodate this many elements,
given the specified load factor.loadFactor
- the load factor (table density) for
establishing the initial table sizejava.lang.IllegalArgumentException
- if the initial capacity of
elements is negative or the load factor is nonpositiveConcurrentHashMapV8(int initialCapacity, float loadFactor, int concurrencyLevel)
initialCapacity
), table
density (loadFactor
), and number of concurrently
updating threads (concurrencyLevel
).initialCapacity
- the initial capacity. The implementation
performs internal sizing to accommodate this many elements,
given the specified load factor.loadFactor
- the load factor (table density) for
establishing the initial table sizeconcurrencyLevel
- the estimated number of concurrently
updating threads. The implementation may use this value as
a sizing hint.java.lang.IllegalArgumentException
- if the initial capacity is
negative or the load factor or concurrencyLevel are
nonpositivestatic final int spread(int h)
private static final int tableSizeFor(int c)
static java.lang.Class<?> comparableClassFor(java.lang.Object x)
static int compareComparables(java.lang.Class<?> kc, java.lang.Object k, java.lang.Object x)
static final <K,V> ConcurrentHashMapV8.Node<K,V> tabAt(ConcurrentHashMapV8.Node<K,V>[] tab, int i)
static final <K,V> boolean casTabAt(ConcurrentHashMapV8.Node<K,V>[] tab, int i, ConcurrentHashMapV8.Node<K,V> c, ConcurrentHashMapV8.Node<K,V> v)
static final <K,V> void setTabAt(ConcurrentHashMapV8.Node<K,V>[] tab, int i, ConcurrentHashMapV8.Node<K,V> v)
public int size()
public boolean isEmpty()
public V get(java.lang.Object key)
null
if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key
k
to a value v
such that key.equals(k)
,
then this method returns v
; otherwise it returns
null
. (There can be at most one such mapping.)
public boolean containsKey(java.lang.Object key)
containsKey
in interface java.util.Map<K,V>
containsKey
in class java.util.AbstractMap<K,V>
key
- possible keytrue
if and only if the specified object
is a key in this table, as determined by the
equals
method; false
otherwisejava.lang.NullPointerException
- if the specified key is nullpublic boolean containsValue(java.lang.Object value)
true
if this map maps one or more keys to the
specified value. Note: This method may require a full traversal
of the map, and is much slower than method containsKey
.containsValue
in interface java.util.Map<K,V>
containsValue
in class java.util.AbstractMap<K,V>
value
- value whose presence in this map is to be testedtrue
if this map maps one or more keys to the
specified valuejava.lang.NullPointerException
- if the specified value is nullpublic V put(K key, V value)
The value can be retrieved by calling the get
method
with a key that is equal to the original key.
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keykey
, or
null
if there was no mapping for key
java.lang.NullPointerException
- if the specified key or value is nullpublic void putAll(java.util.Map<? extends K,? extends V> m)
public V remove(java.lang.Object key)
remove
in interface java.util.Map<K,V>
remove
in class java.util.AbstractMap<K,V>
key
- the key that needs to be removedkey
, or
null
if there was no mapping for key
java.lang.NullPointerException
- if the specified key is nullfinal V replaceNode(java.lang.Object key, V value, java.lang.Object cv)
public void clear()
public ConcurrentHashMapV8.KeySetView<K,V> keySet()
Set
view of the keys contained in this map.
The set is backed by the map, so changes to the map are
reflected in the set, and vice-versa. The set supports element
removal, which removes the corresponding mapping from this map,
via the Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
operations. It does not support the add
or
addAll
operations.
The view's iterator
is a "weakly consistent" iterator
that will never throw ConcurrentModificationException
,
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
public java.util.Collection<V> values()
Collection
view of the values contained in this map.
The collection is backed by the map, so changes to the map are
reflected in the collection, and vice-versa. The collection
supports element removal, which removes the corresponding
mapping from this map, via the Iterator.remove
,
Collection.remove
, removeAll
,
retainAll
, and clear
operations. It does not
support the add
or addAll
operations.
The view's iterator
is a "weakly consistent" iterator
that will never throw ConcurrentModificationException
,
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Set
view of the mappings contained in this map.
The set is backed by the map, so changes to the map are
reflected in the set, and vice-versa. The set supports element
removal, which removes the corresponding mapping from the map,
via the Iterator.remove
, Set.remove
,
removeAll
, retainAll
, and clear
operations.
The view's iterator
is a "weakly consistent" iterator
that will never throw ConcurrentModificationException
,
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
public int hashCode()
Map
, i.e.,
the sum of, for each key-value pair in the map,
key.hashCode() ^ value.hashCode()
.public java.lang.String toString()
{}
"). Adjacent
mappings are separated by the characters ", "
(comma
and space). Each key-value mapping is rendered as the key
followed by an equals sign ("=
") followed by the
associated value.public boolean equals(java.lang.Object o)
true
if the given object is a map with the same
mappings as this map. This operation may return misleading
results if either map is concurrently modified during execution
of this method.private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
ConcurrentHashMapV8
instance to a
stream (i.e., serializes it).s
- the streamjava.io.IOException
- if an I/O error occursprivate void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException
s
- the streamjava.lang.ClassNotFoundException
- if the class of a serialized object
could not be foundjava.io.IOException
- if an I/O error occurspublic V putIfAbsent(K key, V value)
putIfAbsent
in interface java.util.concurrent.ConcurrentMap<K,V>
putIfAbsent
in interface java.util.Map<K,V>
null
if there was no mapping for the keyjava.lang.NullPointerException
- if the specified key or value is nullpublic boolean remove(java.lang.Object key, java.lang.Object value)
public V replace(K key, V value)
replace
in interface java.util.concurrent.ConcurrentMap<K,V>
replace
in interface java.util.Map<K,V>
null
if there was no mapping for the keyjava.lang.NullPointerException
- if the specified key or value is nullpublic V getOrDefault(java.lang.Object key, V defaultValue)
getOrDefault
in interface java.util.concurrent.ConcurrentMap<K,V>
getOrDefault
in interface java.util.Map<K,V>
key
- the key whose associated value is to be returneddefaultValue
- the value to return if this map contains
no mapping for the given keyjava.lang.NullPointerException
- if the specified key is null@Deprecated public boolean contains(java.lang.Object value)
containsValue(Object)
, and exists solely to ensure
full compatibility with class Hashtable
,
which supported this method prior to introduction of the
Java Collections framework.value
- a value to search fortrue
if and only if some key maps to the
value
argument in this table as
determined by the equals
method;
false
otherwisejava.lang.NullPointerException
- if the specified value is nullpublic java.util.Enumeration<K> keys()
keySet()
public java.util.Enumeration<V> elements()
values()
public long mappingCount()
size()
because a ConcurrentHashMapV8 may
contain more mappings than can be represented as an int. The
value returned is an estimate; the actual count may differ if
there are concurrent insertions or removals.public static <K> ConcurrentHashMapV8.KeySetView<K,java.lang.Boolean> newKeySet()
Set
backed by a ConcurrentHashMapV8
from the given type to Boolean.TRUE
.public static <K> ConcurrentHashMapV8.KeySetView<K,java.lang.Boolean> newKeySet(int initialCapacity)
Set
backed by a ConcurrentHashMapV8
from the given type to Boolean.TRUE
.initialCapacity
- The implementation performs internal
sizing to accommodate this many elements.java.lang.IllegalArgumentException
- if the initial capacity of
elements is negativepublic ConcurrentHashMapV8.KeySetView<K,V> keySet(V mappedValue)
Set
view of the keys in this map, using the
given common mapped value for any additions (i.e., Collection.add(E)
and Collection.addAll(Collection)
).
This is of course only appropriate if it is acceptable to use
the same value for all additions from this view.mappedValue
- the mapped value to use for any additionsjava.lang.NullPointerException
- if the mappedValue is nullprivate final ConcurrentHashMapV8.Node<K,V>[] initTable()
private final void addCount(long x, int check)
x
- the count to addcheck
- if <0, don't check resize, if <= 1 only check if uncontendedfinal ConcurrentHashMapV8.Node<K,V>[] helpTransfer(ConcurrentHashMapV8.Node<K,V>[] tab, ConcurrentHashMapV8.Node<K,V> f)
private final void tryPresize(int size)
size
- number of elements (doesn't need to be perfectly accurate)private final void transfer(ConcurrentHashMapV8.Node<K,V>[] tab, ConcurrentHashMapV8.Node<K,V>[] nextTab)
private final void treeifyBin(ConcurrentHashMapV8.Node<K,V>[] tab, int index)
static <K,V> ConcurrentHashMapV8.Node<K,V> untreeify(ConcurrentHashMapV8.Node<K,V> b)
final long sumCount()
private final void fullAddCount(long x, ConcurrentHashMapV8.CounterHashCode hc, boolean wasUncontended)
private static sun.misc.Unsafe getUnsafe()