K
- the type of the keys in this mapV
- the type of the values in this mappublic final class StaticBucketMap<K,V> extends AbstractIterableMap<K,V>
java.util.Map
that performs well in in a highly
thread-contentious environment. The map supports very efficient
get
, put
,
remove
and containsKey
operations, assuming (approximate) uniform hashing and
that the number of entries does not exceed the number of buckets. If the
number of entries exceeds the number of buckets or if the hash codes of the
objects are not uniformly distributed, these operations have a worst case
scenario that is proportional to the number of elements in the map
(O(n)).
Each bucket in the hash table has its own monitor, so two threads can
safely operate on the map at the same time, often without incurring any
monitor contention. This means that you don't have to wrap instances
of this class with Collections.synchronizedMap(Map)
;
instances are already thread-safe. Unfortunately, however, this means
that this map implementation behaves in ways you may find disconcerting.
Bulk operations, such as putAll
or the
retainAll
operation in collection
views, are not atomic. If two threads are simultaneously
executing
staticBucketMapInstance.putAll(map);and
staticBucketMapInstance.entrySet().removeAll(map.entrySet());then the results are generally random. Those two statement could cancel each other out, leaving
staticBucketMapInstance
essentially
unchanged, or they could leave some random subset of map
in
staticBucketMapInstance
.
Also, much like an encyclopedia, the results of size()
and
isEmpty()
are out-of-date as soon as they are produced.
The iterators returned by the collection views of this class are not
fail-fast. They will never raise a
ConcurrentModificationException
. Keys and values
added to the map after the iterator is created do not necessarily appear
during iteration. Similarly, the iterator does not necessarily fail to
return keys and values that were removed after the iterator was created.
Finally, unlike HashMap
-style implementations, this
class never rehashes the map. The number of buckets is fixed
at construction time and never altered. Performance may degrade if
you do not allocate enough buckets upfront.
The atomic(Runnable)
method is provided to allow atomic iterations
and bulk operations; however, overuse of atomic
will basically result in a map that's slower than an ordinary synchronized
HashMap
.
Use this class if you do not require reliable bulk operations and
iterations, or if you can make your own guarantees about how bulk
operations will affect the map.
Constructor and Description |
---|
StaticBucketMap()
Initializes the map with the default number of buckets (255).
|
StaticBucketMap(int numBuckets)
Initializes the map with a specified number of buckets.
|
Modifier and Type | Method and Description |
---|---|
void |
atomic(Runnable r)
Prevents any operations from occurring on this map while the
given
Runnable executes. |
void |
clear()
Clears the map of all entries.
|
boolean |
containsKey(Object key)
Checks if the map contains the specified key.
|
boolean |
containsValue(Object value)
Checks if the map contains the specified value.
|
Set<Map.Entry<K,V>> |
entrySet()
Gets the entry set.
|
boolean |
equals(Object obj)
Compares this map to another, as per the Map specification.
|
V |
get(Object key)
Gets the value associated with the key.
|
int |
hashCode()
Gets the hash code, as per the Map specification.
|
boolean |
isEmpty()
Checks if the size is currently zero.
|
Set<K> |
keySet()
Gets the key set.
|
V |
put(K key,
V value)
Puts a new key value mapping into the map.
|
void |
putAll(Map<? extends K,? extends V> map)
Puts all the entries from the specified map into this map.
|
V |
remove(Object key)
Removes the specified key from the map.
|
int |
size()
Gets the current size of the map.
|
Collection<V> |
values()
Gets the values.
|
mapIterator
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public StaticBucketMap()
public StaticBucketMap(int numBuckets)
numBuckets
- the number of buckets for this mappublic int size()
Map.size()
public boolean isEmpty()
Map.isEmpty()
public V get(Object key)
key
- the key to retrieveMap.get(Object)
public boolean containsKey(Object key)
key
- the key to checkMap.containsKey(Object)
public boolean containsValue(Object value)
value
- the value to checkMap.containsValue(Object)
public V put(K key, V value)
key
- the key to usevalue
- the value to useMap.put(Object, Object)
public V remove(Object key)
key
- the key to removeMap.remove(Object)
public Set<K> keySet()
Map.keySet()
public Collection<V> values()
Map.values()
public Set<Map.Entry<K,V>> entrySet()
Map.entrySet()
public void putAll(Map<? extends K,? extends V> map)
map
- the map of entries to addMap.putAll(Map)
public void clear()
Map.clear()
public boolean equals(Object obj)
public int hashCode()
public void atomic(Runnable r)
Runnable
executes. This method can be used, for
instance, to execute a bulk operation atomically:
staticBucketMapInstance.atomic(new Runnable() { public void run() { staticBucketMapInstance.putAll(map); } });It can also be used if you need a reliable iterator:
staticBucketMapInstance.atomic(new Runnable() { public void run() { Iterator iterator = staticBucketMapInstance.iterator(); while (iterator.hasNext()) { foo(iterator.next(); } } });Implementation note: This method requires a lot of time and a ton of stack space. Essentially a recursive algorithm is used to enter each bucket's monitor. If you have twenty thousand buckets in your map, then the recursive method will be invoked twenty thousand times. You have been warned.
r
- the code to execute atomicallyCopyright © 2001–2019 The Apache Software Foundation. All rights reserved.