org.apache.commons.collections.map
public final class: StaticBucketMap [javadoc |
source]
java.lang.Object
org.apache.commons.collections.map.StaticBucketMap
All Implemented Interfaces:
Map
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:
Commons - Collections 3.0 (previously in main package v2.1)
- version:
$ - Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
- author:
Berin - Loritsch
- author:
Gerhard - Froehlich
- author:
Michael - A. Smith
- author:
Paul - Jack
- author:
Leo - Sutic
- author:
Janek - Bogucki
- author:
Kazuya - Ujihara
| Method from org.apache.commons.collections.map.StaticBucketMap Summary: |
|---|
|
atomic, clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values |
| Method from org.apache.commons.collections.map.StaticBucketMap Detail: |
public void atomic(Runnable r) {
if (r == null) throw new NullPointerException();
atomic(r, 0);
}
Prevents any operations from occurring on this map while the
given 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. |
public void clear() {
for (int i = 0; i < buckets.length; i++) {
Lock lock = locks[i];
synchronized (lock) {
buckets[i] = null;
lock.size = 0;
}
}
}
Clears the map of all entries. |
public boolean containsKey(Object key) {
int hash = getHash(key);
synchronized (locks[hash]) {
Node n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
return true;
}
n = n.next;
}
}
return false;
}
Checks if the map contains the specified key. |
public boolean containsValue(Object value) {
for (int i = 0; i < buckets.length; i++) {
synchronized (locks[i]) {
Node n = buckets[i];
while (n != null) {
if (n.value == value || (n.value != null && n.value.equals(value))) {
return true;
}
n = n.next;
}
}
}
return false;
}
Checks if the map contains the specified value. |
public Set entrySet() {
return new EntrySet();
}
|
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Map == false) {
return false;
}
Map other = (Map) obj;
return entrySet().equals(other.entrySet());
}
Compares this map to another, as per the Map specification. |
public Object get(Object key) {
int hash = getHash(key);
synchronized (locks[hash]) {
Node n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
return n.value;
}
n = n.next;
}
}
return null;
}
Gets the value associated with the key. |
public int hashCode() {
int hashCode = 0;
for (int i = 0; i < buckets.length; i++) {
synchronized (locks[i]) {
Node n = buckets[i];
while (n != null) {
hashCode += n.hashCode();
n = n.next;
}
}
}
return hashCode;
}
Gets the hash code, as per the Map specification. |
public boolean isEmpty() {
return (size() == 0);
}
Checks if the size is currently zero. |
public Set keySet() {
return new KeySet();
}
|
public Object put(Object key,
Object value) {
int hash = getHash(key);
synchronized (locks[hash]) {
Node n = buckets[hash];
if (n == null) {
n = new Node();
n.key = key;
n.value = value;
buckets[hash] = n;
locks[hash].size++;
return null;
}
// Set n to the last node in the linked list. Check each key along the way
// If the key is found, then change the value of that node and return
// the old value.
for (Node next = n; next != null; next = next.next) {
n = next;
if (n.key == key || (n.key != null && n.key.equals(key))) {
Object returnVal = n.value;
n.value = value;
return returnVal;
}
}
// The key was not found in the current list of nodes, add it to the end
// in a new node.
Node newNode = new Node();
newNode.key = key;
newNode.value = value;
n.next = newNode;
locks[hash].size++;
}
return null;
}
Puts a new key value mapping into the map. |
public void putAll(Map map) {
Iterator i = map.keySet().iterator();
while (i.hasNext()) {
Object key = i.next();
put(key, map.get(key));
}
}
Puts all the entries from the specified map into this map.
This operation is not atomic and may have undesired effects. |
public Object remove(Object key) {
int hash = getHash(key);
synchronized (locks[hash]) {
Node n = buckets[hash];
Node prev = null;
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
// Remove this node from the linked list of nodes.
if (null == prev) {
// This node was the head, set the next node to be the new head.
buckets[hash] = n.next;
} else {
// Set the next node of the previous node to be the node after this one.
prev.next = n.next;
}
locks[hash].size--;
return n.value;
}
prev = n;
n = n.next;
}
}
return null;
}
Removes the specified key from the map. |
public int size() {
int cnt = 0;
for (int i = 0; i < buckets.length; i++) {
cnt += locks[i].size;
}
return cnt;
}
Gets the current size of the map.
The value is computed fresh each time the method is called. |
public Collection values() {
return new Values();
}
|