Class FastMap<K,V>
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<K,
V>
This class represents a Map
collection with real-time behavior. Unless the map's size exceeds its
current capacity, no dynamic memory allocation is ever performed and response time is extremely fast and
consistent.
Our benchmark indicates that FastMap.put(key, value)
is up to 5x faster than java.util.HashMap.put(key, value)
. This
difference is mostly due to the cost of the Map.Entry
allocations that FastMap
avoids by
recycling its entries (see note below).
FastMap
has a predictable iteration order, which is the order in which keys were inserted into the map
(similar to java.util.LinkedHashMap
collection class).
Applications may change the resizing policy of FastMap
by overriding the sizeChanged()
method. For
example, to improve predictability, automatic resizing can be disabled.
This implementation is not synchronized. Multiple threads accessing or modifying the collection must be synchronized externally.
Note: To avoid dynamic memory allocations, FastMap
maintains an internal pool of
Map.Entry
objects. The size of the pool is determined by the map's capacity. When an entry is removed
from the map, it is automatically restored to the pool.
This class is public domain (not copyrighted).
- Version:
- 5.3, October 31 2003
- Author:
- Jean-Marie Dautelle
- See Also:
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Returns the capacity of thisFastMap
.void
clear()
Removes all mappings from thisFastMap
.clone()
Returns a shallow copy of thisFastMap
.boolean
containsKey
(Object key) Indicates if thisFastMap
contains a mapping for the specified key.boolean
containsValue
(Object value) Indicates if thisFastMap
maps one or more keys to the specified value.entrySet()
Returns a collection view of the mappings contained in thisFastMap
.boolean
Compares the specified object with thisFastMap
for equality.Returns the value to which thisFastMap
maps the specified key.Returns the entry with the specified key.int
hashCode()
Returns the hash code value for thisFastMap
.boolean
isEmpty()
Indicates if thisFastMap
contains no key-value mappings.keySet()
Returns a set view of the keys contained in thisFastMap
.Associates the specified value with the specified key in thisFastMap
.void
Copies all of the mappings from the specified map to thisFastMap
.Removes the mapping for this key from thisFastMap
if present.void
setCapacity
(int newCapacity) Changes the current capacity of thisFastMap
.int
size()
Returns the number of key-value mappings in thisFastMap
.protected void
This methods is being called when the size of thisFastMap
has changed.toString()
Returns aString
representation of thisFastMap
.values()
Returns a collection view of the values contained in thisFastMap
.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
FastMap
public FastMap()Creates aFastMap
with a capacity of256
entries. -
FastMap
Creates aFastMap
, copy of the specifiedMap
. If the specified map is not an instance ofFastMap
, the newly created map has a capacity set to the specified map's size. The copy has the same order as the original, regardless of the original map's implementation:TreeMap dictionary = ...; FastMap dictionaryLookup = new FastMap(dictionary);
- Parameters:
map
- the map whose mappings are to be placed in this map.
-
FastMap
public FastMap(int capacity) Creates aFastMap
with the specified capacity. Unless the capacity is exceeded, operations on this map do not allocate entries. For optimum performance, the capacity should be of the same order of magnitude or larger than the expected map's size.- Parameters:
capacity
- the number of buckets in the hash table; it also defines the number of pre-allocated entries.
-
-
Method Details
-
size
-
capacity
public int capacity()Returns the capacity of thisFastMap
. The capacity defines the number of buckets in the hash table, as well as the maximum number of entries the map may contain without allocating memory.- Returns:
- this map's capacity.
-
isEmpty
-
containsKey
Indicates if thisFastMap
contains a mapping for the specified key.- Specified by:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- the key whose presence in this map is to be tested.- Returns:
true
if this map contains a mapping for the specified key;false
otherwise.- Throws:
NullPointerException
- if the key isnull
.
-
containsValue
Indicates if thisFastMap
maps one or more keys to the specified value.- Specified by:
containsValue
in interfaceMap<K,
V> - Parameters:
value
- the value whose presence in this map is to be tested.- Returns:
true
if this map maps one or more keys to the specified value.- Throws:
NullPointerException
- if the key isnull
.
-
get
Returns the value to which thisFastMap
maps the specified key.- Specified by:
get
in interfaceMap<K,
V> - Parameters:
key
- the key whose associated value is to be returned.- Returns:
- the value to which this map maps the specified key, or
null
if there is no mapping for the key. - Throws:
NullPointerException
- if key isnull
.
-
getEntry
-
put
Associates the specified value with the specified key in thisFastMap
. If theFastMap
previously contained a mapping for this key, the old value is replaced.- Specified by:
put
in interfaceMap<K,
V> - Parameters:
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.- Returns:
- the previous value associated with specified key, or
null
if there was no mapping for key. Anull
return can also indicate that the map previously associatednull
with the specified key. - Throws:
NullPointerException
- if the key isnull
.
-
putAll
-
remove
Removes the mapping for this key from thisFastMap
if present.- Specified by:
remove
in interfaceMap<K,
V> - Parameters:
key
- the key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or
null
if there was no mapping for key. Anull
return can also indicate that the map previously associatednull
with the specified key. - Throws:
NullPointerException
- if the key isnull
.
-
clear
-
setCapacity
public void setCapacity(int newCapacity) Changes the current capacity of thisFastMap
. If the capacity is increased, new entries are allocated and added to the pool. If the capacity is decreased, entries from the pool are deallocated (and are eventually garbage collected). The capacity also determined the number of buckets for the hash table.- Parameters:
newCapacity
- the new capacity of this map.
-
clone
-
equals
Compares the specified object with thisFastMap
for equality. Returnstrue
if the given object is also a map and the two maps represent the same mappings (regardless of collection iteration order). -
hashCode
-
toString
-
values
Returns a collection view of the values contained in thisFastMap
. 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 this map, via theIterator.remove
,Collection.remove
,removeAll
,retainAll
, andclear
operations. It does not support theadd
oraddAll
operations. -
entrySet
Returns a collection view of the mappings contained in thisFastMap
. Each element in the returned collection is aMap.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 this map, via theIterator.remove
,Collection.remove
,removeAll
,retainAll
, andclear
operations. It does not support theadd
oraddAll
operations. -
keySet
Returns a set view of the keys contained in thisFastMap
. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via theIterator.remove
,Collection.remove
,removeAll
,retainAll
, andclear
operations. It does not support theadd
oraddAll
operations. -
sizeChanged
protected void sizeChanged()This methods is being called when the size of thisFastMap
has changed. The default behavior is to double the map's capacity when the map's size reaches the current map's capacity. Sub-class may override this method to implement custom resizing policies or to disable automatic resizing. For example:Map fixedCapacityMap = new FastMap( 256 ) { protected sizeChanged() { // Do nothing, automatic resizing disabled. } };
- See Also:
-