Class FastMap<K,V>
- java.lang.Object
-
- org.codehaus.plexus.util.FastMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.util.Map<K,V>
public class FastMap<K,V> extends java.lang.Object implements java.util.Map<K,V>, java.lang.Cloneable, java.io.SerializableThis class represents a
Mapcollection 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 thanjava.util.HashMap.put(key, value). This difference is mostly due to the cost of theMap.Entryallocations thatFastMapavoids by recycling its entries (see note below).FastMaphas a predictable iteration order, which is the order in which keys were inserted into the map (similar tojava.util.LinkedHashMapcollection class).Applications may change the resizing policy of
FastMapby overriding thesizeChanged()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,
FastMapmaintains an internal pool ofMap.Entryobjects. 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:
- Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcapacity()Returns the capacity of thisFastMap.voidclear()Removes all mappings from thisFastMap.java.lang.Objectclone()Returns a shallow copy of thisFastMap.booleancontainsKey(java.lang.Object key)Indicates if thisFastMapcontains a mapping for the specified key.booleancontainsValue(java.lang.Object value)Indicates if thisFastMapmaps one or more keys to the specified value.java.util.SetentrySet()Returns a collection view of the mappings contained in thisFastMap.booleanequals(java.lang.Object obj)Compares the specified object with thisFastMapfor equality.Vget(java.lang.Object key)Returns the value to which thisFastMapmaps the specified key.java.util.Map.EntrygetEntry(java.lang.Object key)Returns the entry with the specified key.inthashCode()Returns the hash code value for thisFastMap.booleanisEmpty()Indicates if thisFastMapcontains no key-value mappings.java.util.SetkeySet()Returns a set view of the keys contained in thisFastMap.java.lang.Objectput(java.lang.Object key, java.lang.Object value)Associates the specified value with the specified key in thisFastMap.voidputAll(java.util.Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to thisFastMap.Vremove(java.lang.Object key)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 voidsizeChanged()This methods is being called when the size of thisFastMaphas changed.java.lang.StringtoString()Returns aStringrepresentation of thisFastMap.java.util.Collectionvalues()Returns a collection view of the values contained in thisFastMap.
-
-
-
Constructor Detail
-
FastMap
public FastMap()
Creates aFastMapwith a capacity of256entries.
-
FastMap
public FastMap(java.util.Map map)
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 Detail
-
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
public boolean containsKey(java.lang.Object key)
Indicates if thisFastMapcontains a mapping for the specified key.
-
containsValue
public boolean containsValue(java.lang.Object value)
Indicates if thisFastMapmaps one or more keys to the specified value.
-
get
public V get(java.lang.Object key)
Returns the value to which thisFastMapmaps the specified key.
-
getEntry
public java.util.Map.Entry getEntry(java.lang.Object key)
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
public java.lang.Object put(java.lang.Object key, java.lang.Object value)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 interfacejava.util.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:
java.lang.NullPointerException- if the key isnull.
-
putAll
public void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to thisFastMap.
-
remove
public V remove(java.lang.Object key)
Removes the mapping for this key from thisFastMapif present.- Specified by:
removein interfacejava.util.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:
java.lang.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
public java.lang.Object clone()
Returns a shallow copy of thisFastMap. The keys and the values themselves are not cloned.- Overrides:
clonein classjava.lang.Object- Returns:
- a shallow copy of this map.
-
equals
public boolean equals(java.lang.Object obj)
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
public java.lang.String toString()
Returns aStringrepresentation of thisFastMap.- Overrides:
toStringin classjava.lang.Object- Returns:
this.entrySet().toString();
-
values
public java.util.Collection 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
public java.util.Set 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
public java.util.Set 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:
setCapacity(int)
-
-