org.codehaus.plexus.util
public class FastMap extends Object implements Map, Cloneable, Serializable
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 {@link FastMap#put 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 {@link FastMap} avoids by recycling its entries
(see note below).
{@link 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 {@link FastMap} by overriding the {@link #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, {@link 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
Nested Class Summary | |
---|---|
static class | FastMap.EntryImpl
This class represents a {@link FastMap} entry. |
class | FastMap.EntrySet |
class | FastMap.KeySet |
class | FastMap.Values |
Field Summary | |
---|---|
int | _capacity
Holds the map's current capacity. |
FastMap.EntryImpl[] | _entries
Holds the map's hash table. |
FastMap.EntrySet | _entrySet |
FastMap.KeySet | _keySet |
FastMap.EntryImpl | _mapFirst
Holds the first map entry (linked list). |
FastMap.EntryImpl | _mapLast
Holds the last map entry (linked list). |
int | _mask
Holds the hash code mask. |
FastMap.EntryImpl | _poolFirst
Holds the first pool entry (linked list). |
int | _size
Holds the current size. |
FastMap.Values | _values |
Constructor Summary | |
---|---|
FastMap()
Creates a {@link FastMap} with a capacity of 256 entries. | |
FastMap(Map map)
Creates a {@link FastMap}, copy of the specified Map .
| |
FastMap(int capacity)
Creates a {@link FastMap} with the specified capacity. |
Method Summary | |
---|---|
void | addEntry(Object key, Object value)
Adds a new entry for the specified key and value. |
int | capacity()
Returns the capacity of this {@link FastMap}. |
void | clear()
Removes all mappings from this {@link FastMap}. |
Object | clone()
Returns a shallow copy of this {@link FastMap}. |
boolean | containsKey(Object key)
Indicates if this {@link FastMap} contains a mapping for the specified
key.
|
boolean | containsValue(Object value)
Indicates if this {@link FastMap} maps one or more keys to the
specified value.
|
Set | entrySet()
Returns a collection view of the mappings contained in this
{@link FastMap}. |
boolean | equals(Object obj)
Compares the specified object with this {@link FastMap} for equality.
|
Object | get(Object key)
Returns the value to which this {@link FastMap} maps the specified key.
|
Entry | getEntry(Object key)
Returns the entry with the specified key.
|
int | hashCode()
Returns the hash code value for this {@link FastMap}.
|
void | initialize(int capacity)
Initializes this instance for the specified capacity.
|
boolean | isEmpty()
Indicates if this {@link FastMap} contains no key-value mappings.
|
static int | keyHash(Object key)
Returns the hash code for the specified key. |
Set | keySet()
Returns a set view of the keys contained in this {@link FastMap}.
|
Object | put(Object key, Object value)
Associates the specified value with the specified key in this
{@link FastMap}. |
void | putAll(Map map)
Copies all of the mappings from the specified map to this
{@link FastMap}.
|
void | readObject(ObjectInputStream stream)
Requires special handling during de-serialization process.
|
Object | remove(Object key)
Removes the mapping for this key from this {@link FastMap} if present.
|
void | removeEntry(FastMap.EntryImpl entry)
Removes the specified entry from the map.
|
void | setCapacity(int newCapacity)
Changes the current capacity of this {@link FastMap}. |
int | size()
Returns the number of key-value mappings in this {@link FastMap}.
|
protected void | sizeChanged()
This methods is being called when the size of this {@link FastMap}
has changed. |
String | toString()
Returns a String representation of this {@link FastMap}.
|
Collection | values()
Returns a collection view of the values contained in this
{@link FastMap}. |
void | writeObject(ObjectOutputStream stream)
Requires special handling during serialization process.
|
256
entries.Map
.
If the specified map is not an instance of {@link FastMap}, 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.
Parameters: capacity the number of buckets in the hash table; it also defines the number of pre-allocated entries.
Parameters: key the entry's key. value the entry's value.
Returns: this map's capacity.
Returns: a shallow copy of this map.
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 is null
.
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 is null
.
Map.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 the
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
Returns: a collection view of the mappings contained in this map.
true
if the given object is also a map and the two
maps represent the same mappings (regardless of collection iteration
order).
Parameters: obj the object to be compared for equality with this map.
Returns: true
if the specified object is equal to this map;
false
otherwise.
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 is null
.
Parameters: key the key whose associated entry is to be returned.
Returns: the entry for the specified key or null
if none.
Returns: the hash code value for this map.
Parameters: capacity the initial capacity.
Returns: true
if this map contains no key-value mappings;
false
otherwise.
java.util.HashMap
(ensures similar behavior for ill-conditioned hashcode keys).
Parameters: key the key to calculate the hashcode for.
Returns: the hash code for the specified key.
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
Returns: a set view of the keys contained in this map.
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.
A null
return can also indicate that the map
previously associated null
with the specified key.
Throws: NullPointerException if the key is null
.
Parameters: map the mappings to be stored in this map.
Throws: NullPointerException the specified map is null
, or
the specified map contains null
keys.
Parameters: stream the object input stream.
Throws: IOException if an I/O error occurs. ClassNotFoundException if the class for the object de-serialized is not found.
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.
A null
return can also indicate that the map
previously associated null
with the specified key.
Throws: NullPointerException if the key is null
.
Parameters: entry the entry to be removed.
Parameters: newCapacity the new capacity of this map.
Returns: this map's size.
Map fixedCapacityMap = new FastMap(256) { protected sizeChanged() { // Do nothing, automatic resizing disabled. } };
See Also: FastMap
String
representation of this {@link FastMap}.
Returns: this.entrySet().toString();
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
,
and clear
operations. It does not support the
add
or addAll
operations.
Returns: a collection view of the values contained in this map.
Parameters: stream the object output stream.
Throws: IOException if an I/O error occurs.