Top Description Inners Fields Constructors Methods
org.apache.commons.collections4.map

public final Class StaticBucketMap<K, V>

extends AbstractIterableMap<K, V>
Class Inheritance
Type Parameters
<K>
the type of the keys in this map
<V>
the type of the values in this map
Imports
java.util.AbstractCollection, .AbstractSet, .ArrayList, .Collection, .Iterator, .Map, .NoSuchElementException, .Set, org.apache.commons.collections4.KeyValue

A StaticBucketMap is an efficient, thread-safe implementation of 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 java.util.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 java.util.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 java.util.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 java.util.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.

Since
3.0 (previously in main package v2.1)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class
private class
private class
private class
private static class
StaticBucketMap.Lock

The lock object, which also includes a count of the nodes in this lock.

private static class
StaticBucketMap.Node<K, V>

The Map.Entry for the StaticBucketMap.

private class
private class

Field Summary

Modifier and TypeField and Description
private final StaticBucketMap.Node<K, V>[]
buckets

The array of buckets, where the actual data is held

private static final int
DEFAULT_BUCKETS

The default number of buckets to use

private final StaticBucketMap.Lock[]
locks

The matching array of locks

Constructor Summary

AccessConstructor and Description
public
StaticBucketMap()

Initializes the map with the default number of buckets (255).

public
StaticBucketMap(final int
the number of buckets for this map
numBuckets
)

Initializes the map with a specified number of buckets.

Method Summary

Modifier and TypeMethod and Description
public void
atomic(final Runnable
the code to execute atomically
r
)

Prevents any operations from occurring on this map while the given Runnable executes.

private void
atomic(final Runnable r, final int bucket)

public void
clear()

Implements java.util.Map.clear, org.apache.commons.collections4.Put.clear.

Clears the map of all entries.
public boolean

Returns:

true if found
containsKey
(final Object
the key to check
key
)

Implements java.util.Map.containsKey, org.apache.commons.collections4.Get.containsKey.

Checks if the map contains the specified key.
public boolean

Returns:

true if found
containsValue
(final Object
the value to check
value
)

Implements java.util.Map.containsValue, org.apache.commons.collections4.Get.containsValue.

Checks if the map contains the specified value.
public Set<Map.Entry<K, V>>

Returns:

the entry set
entrySet
()

Implements java.util.Map.entrySet, org.apache.commons.collections4.Get.entrySet.

Gets the entry set.
public boolean

Returns:

true if equal
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Map.equals.

Compares this map to another, as per the Map specification.
public V

Returns:

the associated value
get
(final Object
the key to retrieve
key
)

Implements java.util.Map.get, org.apache.commons.collections4.Get.get.

Gets the value associated with the key.
private int
getHash(final Object key)

Determine the exact hash entry for the key.

public int

Returns:

the hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Implements java.util.Map.hashCode.

Gets the hash code, as per the Map specification.
public boolean

Returns:

true if empty
isEmpty
()

Implements java.util.Map.isEmpty, org.apache.commons.collections4.Get.isEmpty.

Checks if the size is currently zero.
public Set<K>

Returns:

the key set
keySet
()

Implements java.util.Map.keySet, org.apache.commons.collections4.Get.keySet.

Gets the key set.
public V

Returns:

the previous mapping for the key
put
(final K
the key to use
key
,
final V
the value to use
value
)

Implements java.util.Map.put, org.apache.commons.collections4.Put.put.

Puts a new key value mapping into the map.
public void
putAll(final Map<? extends K, ? extends V>
the map of entries to add
map
)

Implements java.util.Map.putAll, org.apache.commons.collections4.Put.putAll.

Puts all the entries from the specified map into this map.
public V

Returns:

the previous value at this key
remove
(final Object
the key to remove
key
)

Implements java.util.Map.remove, org.apache.commons.collections4.Get.remove.

Removes the specified key from the map.
public int

Returns:

the current size
size
()

Implements java.util.Map.size, org.apache.commons.collections4.Get.size.

Gets the current size of the map.
public Collection<V>

Returns:

the values
values
()

Implements java.util.Map.values, org.apache.commons.collections4.Get.values.

Gets the values.
Inherited from org.apache.commons.collections4.map.AbstractIterableMap:
mapIterator