Class StaticBucketMap
- java.lang.Object
-
- org.apache.commons.collections.StaticBucketMap
-
- All Implemented Interfaces:
java.util.Map
@Deprecated(since="2021-04-30") public final class StaticBucketMap extends java.lang.Object implements java.util.Map
Deprecated.Moved to map subpackage. Due to be removed in v4.0.A StaticBucketMap is an efficient, thread-safe implementation ofjava.util.Map
that performs well in in a highly thread-contentious environment. The map supports very efficientget
,put
,remove
andcontainsKey
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 asputAll
or theretainAll
operation in collection views, are not atomic. If two threads are simultaneously executingstaticBucketMapInstance.putAll(map);
andstaticBucketMapInstance.entrySet().removeAll(map.entrySet());
then the results are generally random. Those two statement could cancel each other out, leavingstaticBucketMapInstance
essentially unchanged, or they could leave some random subset ofmap
instaticBucketMapInstance
.Also, much like an encyclopedia, the results of
size()
andisEmpty()
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 ofatomic
will basically result in a map that's slower than an ordinary synchronizedHashMap
. 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 2.1
-
-
Constructor Summary
Constructors Constructor Description StaticBucketMap()
Deprecated.Initializes the map with the default number of buckets (255).StaticBucketMap(int numBuckets)
Deprecated.Initializes the map with a specified number of buckets.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
atomic(java.lang.Runnable r)
Deprecated.Prevents any operations from occurring on this map while the givenRunnable
executes.void
clear()
Deprecated.ImplementsMap.clear()
.boolean
containsKey(java.lang.Object key)
Deprecated.ImplementsMap.containsKey(Object)
.boolean
containsValue(java.lang.Object value)
Deprecated.ImplementsMap.containsValue(Object)
.java.util.Set
entrySet()
Deprecated.ImplementsMap.entrySet()
.boolean
equals(java.lang.Object obj)
Deprecated.ImplementsMap.equals(Object)
.java.lang.Object
get(java.lang.Object key)
Deprecated.ImplementsMap.get(Object)
.int
hashCode()
Deprecated.ImplementsMap.hashCode()
.boolean
isEmpty()
Deprecated.ImplementsMap.isEmpty()
.java.util.Set
keySet()
Deprecated.ImplementsMap.keySet()
.java.lang.Object
put(java.lang.Object key, java.lang.Object value)
Deprecated.ImplementsMap.put(Object, Object)
.void
putAll(java.util.Map other)
Deprecated.ImplementsMap.putAll(Map)
.java.lang.Object
remove(java.lang.Object key)
Deprecated.ImplementsMap.remove(Object)
.int
size()
Deprecated.ImplementsMap.size()
.java.util.Collection
values()
Deprecated.ImplementsMap.values()
.
-
-
-
Constructor Detail
-
StaticBucketMap
public StaticBucketMap()
Deprecated.Initializes the map with the default number of buckets (255).
-
StaticBucketMap
public StaticBucketMap(int numBuckets)
Deprecated.Initializes the map with a specified number of buckets. The number of buckets is never below 17, and is always an odd number (StaticBucketMap ensures this). The number of buckets is inversely proportional to the chances for thread contention. The fewer buckets, the more chances for thread contention. The more buckets the fewer chances for thread contention.- Parameters:
numBuckets
- the number of buckets for this map
-
-
Method Detail
-
keySet
public java.util.Set keySet()
Deprecated.ImplementsMap.keySet()
.- Specified by:
keySet
in interfacejava.util.Map
-
size
public int size()
Deprecated.ImplementsMap.size()
.- Specified by:
size
in interfacejava.util.Map
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)
Deprecated.ImplementsMap.put(Object, Object)
.- Specified by:
put
in interfacejava.util.Map
-
get
public java.lang.Object get(java.lang.Object key)
Deprecated.ImplementsMap.get(Object)
.- Specified by:
get
in interfacejava.util.Map
-
containsKey
public boolean containsKey(java.lang.Object key)
Deprecated.ImplementsMap.containsKey(Object)
.- Specified by:
containsKey
in interfacejava.util.Map
-
containsValue
public boolean containsValue(java.lang.Object value)
Deprecated.ImplementsMap.containsValue(Object)
.- Specified by:
containsValue
in interfacejava.util.Map
-
values
public java.util.Collection values()
Deprecated.ImplementsMap.values()
.- Specified by:
values
in interfacejava.util.Map
-
entrySet
public java.util.Set entrySet()
Deprecated.ImplementsMap.entrySet()
.- Specified by:
entrySet
in interfacejava.util.Map
-
putAll
public void putAll(java.util.Map other)
Deprecated.ImplementsMap.putAll(Map)
.- Specified by:
putAll
in interfacejava.util.Map
-
remove
public java.lang.Object remove(java.lang.Object key)
Deprecated.ImplementsMap.remove(Object)
.- Specified by:
remove
in interfacejava.util.Map
-
isEmpty
public final boolean isEmpty()
Deprecated.ImplementsMap.isEmpty()
.- Specified by:
isEmpty
in interfacejava.util.Map
-
clear
public final void clear()
Deprecated.ImplementsMap.clear()
.- Specified by:
clear
in interfacejava.util.Map
-
equals
public final boolean equals(java.lang.Object obj)
Deprecated.ImplementsMap.equals(Object)
.- Specified by:
equals
in interfacejava.util.Map
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public final int hashCode()
Deprecated.ImplementsMap.hashCode()
.- Specified by:
hashCode
in interfacejava.util.Map
- Overrides:
hashCode
in classjava.lang.Object
-
atomic
public void atomic(java.lang.Runnable r)
Deprecated.Prevents any operations from occurring on this map while the givenRunnable
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.- Parameters:
r
- the code to execute atomically
-
-