Class FastMap<K,V>
- java.lang.Object
-
- javolution37.javolution.realtime.RealtimeObject
-
- javolution37.javolution.util.FastMap<K,V>
-
public class FastMap<K,V> extends RealtimeObject implements java.util.Map<K,V>, Reusable, java.io.Serializable
This class represents a hash map with real-time behavior; smooth capacity increase and no rehashing ever performed.
FastMaphas a predictable iteration order, which is the order in which keys are inserted into the map (similar tojava.util.LinkedHashMapcollection class).FastMap.Entrycan quickly be iterated over (forward or backward) without using iterators. For example:FastMap<String, Thread> map = new FastMap<String, Thread>(); for (FastMap.Entry<String, Thread> e = map.head(), end = map.tail(); (e = e.getNext()) != end;) { String key = e.getKey(); // No typecast necessary. Thread value = e.getValue(); // No typecast necessary. }FastMapmay use custom key comparators; the default comparator is eitherDIRECTorREHASHbased upon the current Javolution Configuration. Users may explicitly set the key comparator toDIRECTfor optimum performance when the hash codes are well distributed for all run-time platforms (e.g. calculated hash codes).Custom key comparators are extremely useful for value retrieval when map's keys and argument keys are not of the same class, such as
StringandText(LEXICAL) or for identity maps (IDENTITY). For example:FastMap identityMap = new FastMap().setKeyComparator(FastComparator.IDENTITY);FastMapmarkedsharedare thread-safe without external synchronization and are often good substitutes forConcurrentHashMap. For example:// Holds the units multiplication lookup table (persistent). static final FastMap<Unit, FastMap<Unit, Unit>> MULT_LOOKUP = new FastMap<Unit, FastMap<Unit, Unit>>("mult-unit-lookup").setShared(true); // Fast and non-blocking (no synchronization necessary). static Unit productOf(Unit left, Unit right) { FastMap<Unit, Unit> leftTable = MULT_LOOKUP.get(left); if (leftTable == null) return calculateProductOf(left, right); Unit result = leftTable.get(right); if (result == null) return calculateProductOf(left, right); return result; // Returns cache result. }Finally,
FastMaparereusable; they maintain an internal pool ofMap.Entryobjects. When an entry is removed from a map, it is automatically restored to its pool (unless the map is shared in which case the removed entry is candidate for garbage collection as it cannot be safely recycled).- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classFastMap.Entry<K,V>This class represents aFastMapentry.-
Nested classes/interfaces inherited from class javolution37.javolution.realtime.RealtimeObject
RealtimeObject.Factory<T extends RealtimeObject>
-
Nested classes/interfaces inherited from interface javolution37.javolution.realtime.Realtime
Realtime.ObjectSpace
-
-
Constructor Summary
Constructors Constructor and Description FastMap()Creates a fast map of small initial capacity.FastMap(int capacity)Creates a map of specified initial capacity; unless the map size reaches the specified capacity, operations on this map will not allocate memory (no lazy object creation).FastMap(java.util.Map<? extends K,? extends V> map)Creates a map containing the specified entries, in the order they are returned by the map iterator.FastMap(java.lang.String id)Creates a persistent map associated to the specified unique identifier (convenience method).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidclear()Removes all map's entries.booleancontainsKey(java.lang.Object key)Indicates if this map contains a mapping for the specified key.booleancontainsValue(java.lang.Object value)Indicates if this map associates one or more keys to the specified value.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Returns aFastCollectionview of the mappings contained in this map.booleanequals(java.lang.Object obj)Compares the specified object with this map for equality.Vget(java.lang.Object key)Returns the value to which this map associates the specified key.FastMap.Entry<K,V>getEntry(java.lang.Object key)Returns the entry with the specified key.FastComparatorgetKeyComparator()Returns the key comparator for this fast map.FastComparatorgetValueComparator()Returns the value comparator for this fast map.inthashCode()Returns the hash code value for this map.FastMap.Entry<K,V>head()Returns the head entry of this map.booleanisEmpty()Indicates if this map contains no key-value mappings.booleanisShared()Indicates if this map supports concurrent operations without synchronization (default unshared).java.util.Set<K>keySet()Returns aFastCollectionview of the keys contained in this map.booleanmove(Realtime.ObjectSpace os)Moves this real-time object to the specified object space.static <K,V> FastMap<K,V>newInstance()Returns a map allocated from the stack when executing in aPoolContext.voidprintStatistics(java.io.PrintStream out)Prints the current statistics on this map.Vput(K key, V value)Associates the specified value with the specified key in this map.voidputAll(java.util.Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to this map.Vremove(java.lang.Object key)Removes the entry for the specified key if present.voidreset()Resets the internal state of this object to its default values.FastMap<K,V>setKeyComparator(FastComparator keyComparator)Sets the key comparator for this fast map.FastMap<K,V>setShared(boolean isShared)Sets the shared status of this map (whether the map is thread-safe or not).FastMap<K,V>setValueComparator(FastComparator valueComparator)Sets the value comparator for this map.intsize()Returns the number of key-value mappings in thisFastMap.FastMap.Entry<K,V>tail()Returns the tail entry of this map.TexttoText()Returns the textual representation of this map.java.util.Map<K,V>unmodifiable()Returns the unmodifiable view associated to this map.java.util.Collection<V>values()Returns aFastCollectionview of the values contained in this map.-
Methods inherited from class javolution37.javolution.realtime.RealtimeObject
export, moveHeap, preserve, toString, unpreserve
-
-
-
-
Constructor Detail
-
FastMap
public FastMap()
Creates a fast map of small initial capacity.
-
FastMap
public FastMap(java.lang.String id)
Creates a persistent map associated to the specified unique identifier (convenience method).- Parameters:
id- the unique identifier for this map.- Throws:
java.lang.IllegalArgumentException- if the identifier is not unique.- See Also:
PersistentReference
-
FastMap
public FastMap(int capacity)
Creates a map of specified initial capacity; unless the map size reaches the specified capacity, operations on this map will not allocate memory (no lazy object creation).- Parameters:
capacity- the initial capacity.
-
-
Method Detail
-
newInstance
public static <K,V> FastMap<K,V> newInstance()
Returns a map allocated from the stack when executing in aPoolContext.- Returns:
- a new, pre-allocated or recycled map instance.
-
head
public final FastMap.Entry<K,V> head()
Returns the head entry of this map.- Returns:
- the entry such as
head().getNext()holds the first map entry.
-
tail
public final FastMap.Entry<K,V> tail()
Returns the tail entry of this map.- Returns:
- the entry such as
tail().getPrevious()holds the last map entry.
-
size
public final int size()
Returns the number of key-value mappings in thisFastMap.
-
isEmpty
public final boolean isEmpty()
Indicates if this map contains no key-value mappings.
-
containsKey
public final boolean containsKey(java.lang.Object key)
Indicates if this map contains a mapping for the specified key.
-
containsValue
public final boolean containsValue(java.lang.Object value)
Indicates if this map associates one or more keys to the specified value.
-
get
public final V get(java.lang.Object key)
Returns the value to which this map associates the specified key.
-
getEntry
public final FastMap.Entry<K,V> 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 final V put(K key, V value)
Associates the specified value with the specified key in this map. If this map previously contained a mapping for this key, the old value is replaced. Forsharedmap internal synchronization is automatically performed.- 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 final void putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map.
-
remove
public final V remove(java.lang.Object key)
Removes the entry for the specified key if present. The entry is recycled if the map is not marked asshared; otherwise the entry is candidate for garbage collection.Note: Shared maps in ImmortalMemory (e.g. static) should not remove their entries as it could cause a memory leak (ImmortalMemory is never garbage collected), instead they should set their entry values to
null.- 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.
-
setShared
public FastMap<K,V> setShared(boolean isShared)
Sets the shared status of this map (whether the map is thread-safe or not). Shared maps are typically used for lookup table (e.g. static instances in ImmortalMemory). They support concurrent access (e.g. iterations) without synchronization, the maps updates themselves are synchronized internally.
Unlike
ConcurrentHashMapaccess to a shared map never blocks. Retrieval reflects the map state not older than the last time the accessing thread has been synchronized (for multi-processors systems synchronizing ensures that the CPU internal cache is not stale).- Parameters:
isShared-trueif this map is shared and thread-safe;falseotherwise.- Returns:
this
-
isShared
public boolean isShared()
Indicates if this map supports concurrent operations without synchronization (default unshared).- Returns:
trueif this map is thread-safe;falseotherwise.
-
setKeyComparator
public FastMap<K,V> setKeyComparator(FastComparator keyComparator)
Sets the key comparator for this fast map.- Parameters:
keyComparator- the key comparator.- Returns:
this
-
getKeyComparator
public FastComparator getKeyComparator()
Returns the key comparator for this fast map.- Returns:
- the key comparator.
-
setValueComparator
public FastMap<K,V> setValueComparator(FastComparator valueComparator)
Sets the value comparator for this map.- Parameters:
valueComparator- the value comparator.- Returns:
this
-
getValueComparator
public FastComparator getValueComparator()
Returns the value comparator for this fast map.- Returns:
- the value comparator.
-
clear
public final void clear()
Removes all map's entries. The entries are removed and recycled; unless this map issharedin which case the entries are candidate for garbage collection.Note: Shared maps in ImmortalMemory (e.g. static) should not remove their entries as it could cause a memory leak (ImmortalMemory is never garbage collected), instead they should set their entry values to
null.
-
equals
public boolean equals(java.lang.Object obj)
Compares the specified object with this map for 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 this map.
-
toText
public Text toText()
Returns the textual representation of this map.- Specified by:
toTextin interfaceRealtime- Overrides:
toTextin classRealtimeObject- Returns:
- the textual representation of the entry set.
-
printStatistics
public void printStatistics(java.io.PrintStream out)
Prints the current statistics on this map. This method may help identify poorly defined hash functions. An average collision of less than50%is typically acceptable.- Parameters:
out- the stream to use for output (e.g.System.out)
-
values
public final java.util.Collection<V> values()
Returns aFastCollectionview of the values contained in this map. 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,retainAllandclearoperations. It does not support theaddoraddAlloperations.- Specified by:
valuesin interfacejava.util.Map<K,V>- Returns:
- a collection view of the values contained in this map
(instance of
FastCollection).
-
entrySet
public final java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns aFastCollectionview of the mappings contained in this map. Each element in the returned collection is aFastMap.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.- Specified by:
entrySetin interfacejava.util.Map<K,V>- Returns:
- a collection view of the mappings contained in this map
(instance of
FastCollection).
-
keySet
public final java.util.Set<K> keySet()
Returns aFastCollectionview of the keys contained in this map. 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.- Specified by:
keySetin interfacejava.util.Map<K,V>- Returns:
- a set view of the keys contained in this map
(instance of
FastCollection).
-
unmodifiable
public final java.util.Map<K,V> unmodifiable()
Returns the unmodifiable view associated to this map. Attempts to modify the returned map or to directly access its (modifiable) map entries (e.g.unmodifiable().entrySet()) result in anUnsupportedOperationExceptionbeing thrown. UnmodifiableFastCollectionviews of this map keys and values are nonetheless obtainable (e.g.unmodifiable().keySet(),unmodifiable().values()).- Returns:
- an unmodifiable view of this map.
-
move
public boolean move(Realtime.ObjectSpace os)
Description copied from interface:RealtimeMoves this real-time object to the specified object space.- Specified by:
movein interfaceRealtime- Overrides:
movein classRealtimeObject- Parameters:
os- the object space to move this real-time object to.- Returns:
trueif the move has to be propagated to external real-time references;falseotherwise.
-
-
DMelt 3.0 © DataMelt by jWork.ORG