K - the type of keys maintained by this mapV - the type of mapped valuespublic class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V> implements ConcurrentNavigableMap<K,V>, Cloneable, Serializable
ConcurrentNavigableMap implementation. This
class maintains a map in ascending key order, sorted according to
the natural order for the key's class (see Comparable), or by the Comparator provided at creation
time, depending on which constructor is used.
This class implements a concurrent variant of SkipLists providing
expected average log(n) time cost for the
containsKey, get, put and
remove operations and their variants. Insertion, removal,
update, and access operations safely execute concurrently by
multiple threads. Iterators are weakly consistent, returning
elements reflecting the state of the map at some point at or since
the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with
other operations. Ascending key ordered views and their iterators
are faster than descending ones.
All Map.Entry pairs returned by methods in this class
and its views represent snapshots of mappings at the time they were
produced. They do not support the Entry.setValue
method. (Note however that it is possible to change mappings in the
associated map using put, putIfAbsent, or
replace, depending on exactly which effect you need.)
Beware that, unlike in most collections, the size
method is not a constant-time operation. Because of the
asynchronous nature of these maps, determining the current number
of elements requires a traversal of the elements. Additionally,
the bulk operations putAll, equals, and
clear are not guaranteed to be performed
atomically. For example, an iterator operating concurrently with a
putAll operation might view only some of the added
elements.
This class and its views and iterators implement all of the
optional methods of the Map and Iterator
interfaces. Like most other concurrent collections, this class does
not permit the use of null keys or values because some
null return values cannot be reliably distinguished from the
absence of elements.
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>| Constructor and Description |
|---|
ConcurrentSkipListMap()
Constructs a new empty map, sorted according to the keys' natural
order.
|
ConcurrentSkipListMap(Comparator<? super K> c)
Constructs a new empty map, sorted according to the given comparator.
|
ConcurrentSkipListMap(Map<? extends K,? extends V> m)
Constructs a new map containing the same mappings as the given map,
sorted according to the keys' natural order.
|
ConcurrentSkipListMap(SortedMap<K,? extends V> m)
Constructs a new map containing the same mappings as the given
SortedMap, sorted according to the same ordering. |
| Modifier and Type | Method and Description |
|---|---|
Map.Entry<K,V> |
ceilingEntry(K key)
Returns a key-value mapping associated with the least key
greater than or equal to the given key, or
null if
there is no such entry. |
K |
ceilingKey(K key)
Returns least key greater than or equal to the given key, or
null if there is no such key. |
void |
clear()
Removes all mappings from this map.
|
Object |
clone()
Returns a shallow copy of this
Map instance. |
Comparator<? super K> |
comparator()
Returns the comparator used to order this map, or
null
if this map uses its keys' natural order. |
boolean |
containsKey(Object key)
Returns
true if this map contains a mapping for the specified
key. |
boolean |
containsValue(Object value)
Returns
true if this map maps one or more keys to the
specified value. |
Set<Map.Entry<K,V>> |
descendingEntrySet()
Returns a collection view of the mappings contained in this
map, in descending order.
|
Set<K> |
descendingKeySet()
Returns a set view of the keys contained in this map in
descending order.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a collection view of the mappings contained in this
map.
|
boolean |
equals(Object o)
Compares the specified object with this map for equality.
|
Map.Entry<K,V> |
firstEntry()
Returns a key-value mapping associated with the least
key in this map, or
null if the map is empty. |
K |
firstKey()
Returns the first (lowest) key currently in this map.
|
Map.Entry<K,V> |
floorEntry(K key)
Returns a key-value mapping associated with the greatest key
less than or equal to the given key, or
null if there
is no such entry. |
K |
floorKey(K key)
Returns the greatest key
less than or equal to the given key, or
null if there
is no such key. |
V |
get(Object key)
Returns the value to which this map maps the specified key.
|
ConcurrentNavigableMap<K,V> |
headMap(K toKey)
Returns a view of the portion of this map whose keys are
strictly less than
toKey. |
Map.Entry<K,V> |
higherEntry(K key)
Returns a key-value mapping associated with the least key
strictly greater than the given key, or
null if there
is no such entry. |
K |
higherKey(K key)
Returns the least key strictly greater than the given key, or
null if there is no such key. |
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
Set<K> |
keySet()
Returns a set view of the keys contained in this map.
|
Map.Entry<K,V> |
lastEntry()
Returns a key-value mapping associated with the greatest
key in this map, or
null if the map is empty. |
K |
lastKey()
Returns the last (highest) key currently in this map.
|
Map.Entry<K,V> |
lowerEntry(K key)
Returns a key-value mapping associated with the greatest
key strictly less than the given key, or
null if there is no
such entry. |
K |
lowerKey(K key)
Returns the greatest key strictly less than the given key, or
null if there is no such key. |
Map.Entry<K,V> |
pollFirstEntry()
Removes and returns a key-value mapping associated with
the least key in this map, or
null if the map is empty. |
Map.Entry<K,V> |
pollLastEntry()
Removes and returns a key-value mapping associated with
the greatest key in this map, or
null if the map is empty. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
V |
putIfAbsent(K key,
V value)
If the specified key is not already associated
with a value, associate it with the given value.
|
V |
remove(Object key)
Removes the mapping for this key from this Map if present.
|
boolean |
remove(Object key,
Object value)
Removes entry for key only if currently mapped to given value.
|
V |
replace(K key,
V value)
Replaces entry for key only if currently mapped to some value.
|
boolean |
replace(K key,
V oldValue,
V newValue)
Replaces entry for key only if currently mapped to given value.
|
int |
size()
Returns the number of elements in this map.
|
ConcurrentNavigableMap<K,V> |
subMap(K fromKey,
K toKey)
Returns a view of the portion of this map whose keys range from
fromKey, inclusive, to toKey, exclusive. |
ConcurrentNavigableMap<K,V> |
tailMap(K fromKey)
Returns a view of the portion of this map whose keys are
greater than or equal to
fromKey. |
Collection<V> |
values()
Returns a collection view of the values contained in this map.
|
hashCode, putAll, toStringpublic ConcurrentSkipListMap()
public ConcurrentSkipListMap(Comparator<? super K> c)
c - the comparator that will be used to sort this map. A
null value indicates that the keys' natural
ordering should be used.public ConcurrentSkipListMap(Map<? extends K,? extends V> m)
m - the map whose mappings are to be placed in this mapClassCastException - if the keys in m are not Comparable, or
are not mutually comparableNullPointerException - if the specified map is nullpublic ConcurrentSkipListMap(SortedMap<K,? extends V> m)
SortedMap, sorted according to the same ordering.m - the sorted map whose mappings are to be placed in this
map, and whose comparator is to be used to sort this mapNullPointerException - if the specified sorted map is
nullpublic Object clone()
Map instance. (The keys and
values themselves are not cloned.)clone in class AbstractMap<K,V>public boolean containsKey(Object key)
true if this map contains a mapping for the specified
key.containsKey in interface Map<K,V>containsKey in class AbstractMap<K,V>key - key whose presence in this map is to be testedtrue if this map contains a mapping for the
specified keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key is nullpublic V get(Object key)
null if the map contains no mapping for this key.get in interface Map<K,V>get in class AbstractMap<K,V>key - key whose associated value is to be returnednull if the map contains no mapping for the keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key is nullpublic V put(K key, V value)
put in interface Map<K,V>put in class AbstractMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keynull
if there was no mapping for keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key or value are nullpublic V remove(Object key)
remove in interface Map<K,V>remove in class AbstractMap<K,V>key - key for which mapping should be removednull
if there was no mapping for keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key is nullpublic boolean containsValue(Object value)
true if this map maps one or more keys to the
specified value. This operation requires time linear in the
Map size.containsValue in interface Map<K,V>containsValue in class AbstractMap<K,V>value - value whose presence in this Map is to be testedtrue if a mapping to value exists;
false otherwiseNullPointerException - if the value is nullpublic int size()
Integer.MAX_VALUE elements, it
returns Integer.MAX_VALUE.
Beware that, unlike in most collections, this method is NOT a constant-time operation. Because of the asynchronous nature of these maps, determining the current number of elements requires traversing them all to count them. Additionally, it is possible for the size to change during execution of this method, in which case the returned result will be inaccurate. Thus, this method is typically not very useful in concurrent applications.
public boolean isEmpty()
true if this map contains no key-value mappings.public void clear()
public Set<K> keySet()
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 Set<K> descendingKeySet()
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.descendingKeySet in interface NavigableMap<K,V>public Collection<V> values()
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 Set<Map.Entry<K,V>> entrySet()
Map.Entry. 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 the 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. The
Map.Entry elements returned by
iterator.next() do not support the
setValue operation.public Set<Map.Entry<K,V>> descendingEntrySet()
Map.Entry. 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 the 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. The
Map.Entry elements returned by
iterator.next() do not support the
setValue operation.descendingEntrySet in interface NavigableMap<K,V>public boolean equals(Object o)
true if the given object is also a map and the
two maps represent the same mappings. More formally, two maps
t1 and t2 represent the same mappings if
t1.keySet().equals(t2.keySet()) and for every key
k in t1.keySet(), (t1.get(k)==null ?
t2.get(k)==null : t1.get(k).equals(t2.get(k))) . This
operation may return misleading results if either map is
concurrently modified during execution of this method.public V putIfAbsent(K key, V value)
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);
except that the action is performed atomically.putIfAbsent in interface ConcurrentMap<K,V>key - key with which the specified value is to be associatedvalue - value to be associated with the specified keynull
if there was no mapping for keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key or value are nullpublic boolean remove(Object key, Object value)
if ((map.containsKey(key) && map.get(key).equals(value)) {
map.remove(key);
return true;
} else return false;
except that the action is performed atomically.remove in interface ConcurrentMap<K,V>key - key with which the specified value is associatedvalue - value associated with the specified keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key or value are nullpublic boolean replace(K key, V oldValue, V newValue)
if ((map.containsKey(key) && map.get(key).equals(oldValue)) {
map.put(key, newValue);
return true;
} else return false;
except that the action is performed atomically.replace in interface ConcurrentMap<K,V>key - key with which the specified value is associatedoldValue - value expected to be associated with the specified keynewValue - value to be associated with the specified keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if key, oldValue or newValue are
nullpublic V replace(K key, V value)
if ((map.containsKey(key)) {
return map.put(key, value);
} else return null;
except that the action is performed atomically.replace in interface ConcurrentMap<K,V>key - key with which the specified value is associatedvalue - value to be associated with the specified keynull
if there was no mapping for keyClassCastException - if the key cannot be compared with the keys
currently in the mapNullPointerException - if the key or value are nullpublic Comparator<? super K> comparator()
null
if this map uses its keys' natural order.comparator in interface SortedMap<K,V>null if it uses its keys' natural sort methodpublic K firstKey()
firstKey in interface SortedMap<K,V>NoSuchElementException - Map is emptypublic K lastKey()
lastKey in interface SortedMap<K,V>NoSuchElementException - Map is emptypublic ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey)
fromKey, inclusive, to toKey, exclusive. (If
fromKey and toKey are equal, the returned sorted map
is empty.) The returned sorted map is backed by this map, so changes
in the returned sorted map are reflected in this map, and vice-versa.subMap in interface SortedMap<K,V>subMap in interface ConcurrentNavigableMap<K,V>subMap in interface NavigableMap<K,V>fromKey - low endpoint (inclusive) of the subMaptoKey - high endpoint (exclusive) of the subMapfromKey, inclusive, to toKey, exclusiveClassCastException - if fromKey and toKey
cannot be compared to one another using this map's comparator
(or, if the map has no comparator, using natural ordering)IllegalArgumentException - if fromKey is greater than
toKeyNullPointerException - if fromKey or toKey is
nullpublic ConcurrentNavigableMap<K,V> headMap(K toKey)
toKey. The returned sorted map is
backed by this map, so changes in the returned sorted map are
reflected in this map, and vice-versa.headMap in interface SortedMap<K,V>headMap in interface ConcurrentNavigableMap<K,V>headMap in interface NavigableMap<K,V>toKey - high endpoint (exclusive) of the headMaptoKeyClassCastException - if toKey is not compatible
with this map's comparator (or, if the map has no comparator,
if toKey does not implement Comparable)NullPointerException - if toKey is nullpublic ConcurrentNavigableMap<K,V> tailMap(K fromKey)
fromKey. The returned sorted
map is backed by this map, so changes in the returned sorted
map are reflected in this map, and vice-versa.tailMap in interface SortedMap<K,V>tailMap in interface ConcurrentNavigableMap<K,V>tailMap in interface NavigableMap<K,V>fromKey - low endpoint (inclusive) of the tailMapfromKeyClassCastException - if fromKey is not
compatible with this map's comparator (or, if the map has no
comparator, if fromKey does not implement
Comparable)NullPointerException - if fromKey is nullpublic Map.Entry<K,V> ceilingEntry(K key)
null if
there is no such entry. The returned entry does not
support the Entry.setValue method.ceilingEntry in interface NavigableMap<K,V>key - the keynull if there is no such EntryClassCastException - if key cannot be compared with the
keys currently in the mapNullPointerException - if key is nullpublic K ceilingKey(K key)
null if there is no such key.ceilingKey in interface NavigableMap<K,V>key - the keynull
if there is no such keyClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic Map.Entry<K,V> lowerEntry(K key)
null if there is no
such entry. The returned entry does not support
the Entry.setValue method.lowerEntry in interface NavigableMap<K,V>key - the keynull if there is no such EntryClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic K lowerKey(K key)
null if there is no such key.lowerKey in interface NavigableMap<K,V>key - the keynull if there is no such keyClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic Map.Entry<K,V> floorEntry(K key)
null if there
is no such entry. The returned entry does not support
the Entry.setValue method.floorEntry in interface NavigableMap<K,V>key - the keynull
if there is no such EntryClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic K floorKey(K key)
null if there
is no such key.floorKey in interface NavigableMap<K,V>key - the keynull if there is no
such keyClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic Map.Entry<K,V> higherEntry(K key)
null if there
is no such entry. The returned entry does not support
the Entry.setValue method.higherEntry in interface NavigableMap<K,V>key - the keynull if there is no such EntryClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic K higherKey(K key)
null if there is no such key.higherKey in interface NavigableMap<K,V>key - the keynull if there is no such keyClassCastException - if key cannot be compared with the keys
currently in the mapNullPointerException - if key is nullpublic Map.Entry<K,V> firstEntry()
null if the map is empty.
The returned entry does not support
the Entry.setValue method.firstEntry in interface NavigableMap<K,V>null
if the map is emptypublic Map.Entry<K,V> lastEntry()
null if the map is empty.
The returned entry does not support
the Entry.setValue method.lastEntry in interface NavigableMap<K,V>null
if the map is emptypublic Map.Entry<K,V> pollFirstEntry()
null if the map is empty.
The returned entry does not support
the Entry.setValue method.pollFirstEntry in interface NavigableMap<K,V>null
if the map is emptypublic Map.Entry<K,V> pollLastEntry()
null if the map is empty.
The returned entry does not support
the Entry.setValue method.pollLastEntry in interface NavigableMap<K,V>null
if the map is empty