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 SummaryConstructors
- 
Method SummaryModifier 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.Objectfinalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
- 
Constructor Details- 
FastMappublic FastMap()Creates aFastMapwith a capacity of256entries.
- 
FastMapCreates 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.
 
- 
FastMappublic 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- 
sizepublic int size()Returns the number of key-value mappings in thisFastMap.
- 
capacitypublic 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.
 
- 
isEmptypublic boolean isEmpty()Indicates if thisFastMapcontains no key-value mappings.
- 
containsKeyIndicates if thisFastMapcontains a mapping for the specified key.- Specified by:
- containsKeyin interface- Map<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 is- null.
 
- 
containsValueIndicates if thisFastMapmaps one or more keys to the specified value.- Specified by:
- containsValuein interface- Map<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 is- null.
 
- 
getReturns the value to which thisFastMapmaps the specified key.- Specified by:
- getin interface- Map<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 is- null.
 
- 
getEntryReturns 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.
 
- 
putAssociates 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 interface- Map<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 is- null.
 
- 
putAllCopies all of the mappings from the specified map to thisFastMap.- Specified by:
- putAllin interface- Map<K,- V> 
- Parameters:
- map- the mappings to be stored in this map.
- Throws:
- NullPointerException- the specified map is- null, or the specified map contains- nullkeys.
 
- 
removeRemoves the mapping for this key from thisFastMapif present.- Specified by:
- removein interface- Map<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 is- null.
 
- 
clearpublic void clear()Removes all mappings from thisFastMap.
- 
setCapacitypublic 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.
 
- 
cloneReturns a shallow copy of thisFastMap. The keys and the values themselves are not cloned.
- 
equalsCompares 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).
- 
hashCodepublic int hashCode()Returns the hash code value for thisFastMap.
- 
toStringReturns aStringrepresentation of thisFastMap.
- 
valuesReturns 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.
- 
entrySetReturns 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.
- 
keySetReturns 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.
- 
sizeChangedprotected 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:
 
 
-