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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the capacity of thisFastMap.voidclear()Removes all mappings from thisFastMap.clone()Returns a shallow copy of thisFastMap.booleancontainsKey(Object key) Indicates if thisFastMapcontains a mapping for the specified key.booleancontainsValue(Object value) Indicates if thisFastMapmaps one or more keys to the specified value.entrySet()Returns a collection view of the mappings contained in thisFastMap.booleanCompares the specified object with thisFastMapfor equality.Returns the value to which thisFastMapmaps the specified key.Returns the entry with the specified key.inthashCode()Returns the hash code value for thisFastMap.booleanisEmpty()Indicates if thisFastMapcontains 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.voidCopies all of the mappings from the specified map to thisFastMap.Removes the mapping for this key from thisFastMapif present.voidsetCapacity(int newCapacity) Changes the current capacity of thisFastMap.intsize()Returns the number of key-value mappings in thisFastMap.protected voidThis methods is being called when the size of thisFastMaphas changed.toString()Returns aStringrepresentation 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, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Constructor Details
-
FastMap
public FastMap()Creates aFastMapwith a capacity of256entries. -
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 aFastMapwith 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
public int size()Returns the number of key-value mappings in thisFastMap. -
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
public boolean isEmpty()Indicates if thisFastMapcontains no key-value mappings. -
containsKey
Indicates if thisFastMapcontains a mapping for the specified key.- Specified by:
containsKeyin interfaceMap<K,V> - Parameters:
key- the key whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified key;falseotherwise.- Throws:
NullPointerException- if the key isnull.
-
containsValue
Indicates if thisFastMapmaps one or more keys to the specified value.- Specified by:
containsValuein interfaceMap<K,V> - Parameters:
value- the value whose presence in this map is to be tested.- Returns:
trueif this map maps one or more keys to the specified value.- Throws:
NullPointerException- if the key isnull.
-
get
Returns the value to which thisFastMapmaps the specified key.- Specified by:
getin 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
nullif there is no mapping for the key. - Throws:
NullPointerException- if key isnull.
-
getEntry
Returns the entry with the specified key.- Parameters:
key- the key whose associated entry is to be returned.- Returns:
- the entry for the specified key or
nullif none.
-
put
Associates the specified value with the specified key in thisFastMap. If theFastMappreviously contained a mapping for this key, the old value is replaced.- Specified by:
putin 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
nullif there was no mapping for key. Anullreturn can also indicate that the map previously associatednullwith the specified key. - Throws:
NullPointerException- if the key isnull.
-
putAll
Copies all of the mappings from the specified map to thisFastMap.- Specified by:
putAllin interfaceMap<K,V> - Parameters:
map- the mappings to be stored in this map.- Throws:
NullPointerException- the specified map isnull, or the specified map containsnullkeys.
-
remove
Removes the mapping for this key from thisFastMapif present.- Specified by:
removein interfaceMap<K,V> - Parameters:
key- the key whose mapping is to be removed from the map.- Returns:
- previous value associated with specified key, or
nullif there was no mapping for key. Anullreturn can also indicate that the map previously associatednullwith the specified key. - Throws:
NullPointerException- if the key isnull.
-
clear
public void clear()Removes all mappings from thisFastMap. -
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
Returns a shallow copy of thisFastMap. The keys and the values themselves are not cloned. -
equals
Compares the specified object with thisFastMapfor equality. Returnstrueif the given object is also a map and the two maps represent the same mappings (regardless of collection iteration order). -
hashCode
public int hashCode()Returns the hash code value for thisFastMap. -
toString
Returns aStringrepresentation of thisFastMap. -
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, andclearoperations. It does not support theaddoraddAlloperations. -
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, andclearoperations. It does not support theaddoraddAlloperations. -
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, andclearoperations. It does not support theaddoraddAlloperations. -
sizeChanged
protected void sizeChanged()This methods is being called when the size of thisFastMaphas 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:
-