@Realtime public class FastMap<K,V> extends Object implements Map<K,V>, ConcurrentMap<K,V>, Serializable
A high-performance hash map with real-time
behavior.
Related to FastCollection
, fast map supports various views.
atomic()
- Thread-safe view for which all reads are mutex-free
and map updates (e.g. putAll
) are atomic.shared()
- View allowing concurrent modifications.parallel()
- A view allowing parallel processing including updates
.sequential()
- View disallowing parallel processing.unmodifiable()
- View which does not allow any modifications.entrySet()
- FastSet
view over the map entries allowing
entries to be added/removed.keySet()
- FastSet
view over the map keys allowing keys
to be added (map entry with null
value).values()
- FastCollection
view over the map values (add not supported). The iteration order over the map keys, values or entries is deterministic
(unlike HashMap
). It is either the insertion order (default)
or the key order for the FastSortedMap
subclass.
This class permits null
keys.
Fast maps can advantageously replace any of the standard java.util
maps.
FastMap<Foo, Bar> hashMap = new FastMap<Foo, Bar>(); FastMap<Foo, Bar> concurrentHashMap = new FastMap<Foo, Bar>().shared(); // FastMap implements ConcurrentMap interface. FastMap<Foo, Bar> linkedHashMap = new FastMap<Foo, Bar>(); // Deterministic iteration order (insertion order). FastMap<Foo, Bar> treeMap = new FastSortedMap<Foo, Bar>(); FastMap<Foo, Bar> concurrentSkipListMap = new FastSortedMap<Foo, Bar>().shared(); FastMap<Foo, Bar> identityHashMap = new FastMap<Foo, Bar>(Equalities.IDENTITY);
and adds more ...
FastMap<Foo, Bar> atomicMap = new FastMap<Foo, Bar>().atomic(); // Mutex-free access, all updates (e.g. putAll) atomics (unlike ConcurrentHashMap). FastMap<Foo, Bar> atomicTree = new FastSortedMap<Foo, Bar>().atomic(); // Mutex-free access, all updates (e.g. putAll) atomics. FastMap<Foo, Bar> parallelMap = new FastMap<Foo, Bar>().parallel(); // Map actions (perform/update) performed concurrently. FastMap<Foo, Bar> linkedConcurrentHashMap = new FastMap<Foo, Bar>().shared(); // No equivalent in java.util ! FastMap<String, Bar> lexicalHashMap = new FastMap<String, Bar>(Equalities.LEXICAL); // Allows for value retrieval using any CharSequence key. FastMap<String, Bar> fastStringHashMap = new FastMap<String, Bar>(Equalities.LEXICAL_FAST); // Same with faster hashcode calculations. ...
Of course all views (entry, key, values) over a fast map are fast collections and allow parallel processing.
Consumer<Collection<String>> removeNull = new Consumer<Collection<String>>() { public void accept(Collection<String> view) { Iterator<String> it = view.iterator(); while (it.hasNext()) { if (it.next() == null) it.remove(); } } }; FastMap<Person, String> names = ... names.values().update(removeNull); // Remove all entries with null values. names.atomic().values().update(removeNull); // Same but performed atomically. names.parallel().values().update(removeNull); // Same but performed in parallel.
Modifier | Constructor and Description |
---|---|
|
FastMap()
Creates an empty fast map.
|
|
FastMap(Equality<? super K> keyEquality)
Creates an empty fast map using the specified comparator for keys
equality.
|
|
FastMap(Equality<? super K> keyEquality,
Equality<? super V> valueEquality)
Creates an empty fast map using the specified comparators for keys
equality and values equality.
|
protected |
FastMap(MapService<K,V> service)
Creates a map backed up by the specified service implementation.
|
Modifier and Type | Method and Description |
---|---|
FastMap<K,V> |
atomic()
Returns an atomic view over this map.
|
void |
clear()
Removes all this map's entries.
|
boolean |
containsKey(Object key)
Indicates if this map contains the specified key.
|
boolean |
containsValue(Object value)
Indicates if this map contains the specified value.
|
FastSet<Map.Entry<K,V>> |
entrySet()
Returns a set view of the mappings contained in
this map.
|
V |
get(Object key)
Returns the value for the specified key.
|
boolean |
isEmpty()
Indicates if this map is empty
|
FastSet<K> |
keySet()
Returns a set view of the keys contained in this map.
|
FastMap<K,V> |
parallel()
Returns a parallel map.
|
void |
perform(Consumer<? extends Map<K,V>> action)
Executes the specified read-only action on this map.
|
V |
put(K key,
V value)
Associates the specified value with the specified key.
|
FastMap<K,V> |
putAll(FastMap<? extends K,? extends V> that)
Returns this map with the specified map's entries added.
|
void |
putAll(Map<? extends K,? extends V> map)
Adds the specified map entries to this map.
|
V |
putIfAbsent(K key,
V value)
Associates the specified value with the specified key only if the
specified key has no current mapping.
|
V |
remove(Object key)
Removes the entry for the specified key.
|
boolean |
remove(Object key,
Object value)
Removes the entry for a key only if currently mapped to a given value.
|
V |
replace(K key,
V value)
Replaces the entry for a key only if currently mapped to some value.
|
boolean |
replace(K key,
V oldValue,
V newValue)
Replaces the entry for a key only if currently mapped to a given value.
|
FastMap<K,V> |
sequential()
Returns a sequential view of this collection.
|
protected MapService<K,V> |
service()
Returns this map service implementation.
|
FastMap<K,V> |
shared()
Returns a thread-safe view over this map.
|
int |
size()
Returns the number of entries/keys/values in this map.
|
<T extends Map<K,V>> |
toImmutable()
Returns an immutable reference over this map.
|
String |
toString()
Returns the string representation of this map entries.
|
FastMap<K,V> |
unmodifiable()
Returns an unmodifiable view over this map.
|
void |
update(Consumer<? extends Map<K,V>> action)
Executes the specified update action on this map.
|
FastCollection<V> |
values()
Returns a collection view of the values contained in this map.
|
public FastMap()
public FastMap(Equality<? super K> keyEquality)
public FastMap(Equality<? super K> keyEquality, Equality<? super V> valueEquality)
protected FastMap(MapService<K,V> service)
@Parallelizable(mutexFree=true, comment="Except for write operations, all read operations are mutex-free.") public FastMap<K,V> atomic()
ConcurrentModificationException
possible).@Parallelizable(mutexFree=false, comment="Use multiple-readers/single-writer lock.") public FastMap<K,V> shared()
ConcurrentModificationException
possible).public FastMap<K,V> parallel()
public FastMap<K,V> sequential()
public FastMap<K,V> unmodifiable()
UnsupportedOperationException
being raised.public FastSet<K> keySet()
null
.public FastCollection<V> values()
public FastSet<Map.Entry<K,V>> entrySet()
@Realtime(limit=LINEAR) public void perform(Consumer<? extends Map<K,V>> action)
parallel
.action
- the read-only action.UnsupportedOperationException
- if the action tries to update
this map.ClassCastException
- if the action type is not compatible with
this map (e.g. action on sorted map and this is a hash map).update(Consumer)
@Realtime(limit=LINEAR) public void update(Consumer<? extends Map<K,V>> action)
parallel
.
For atomic
maps the update is atomic (either concurrent
readers see the full result of the action or nothing).action
- the update action.ClassCastException
- if the action type is not compatible with
this map (e.g. action on sorted map and this is a hash map).perform(Consumer)
@Realtime(limit=CONSTANT) public int size()
@Realtime(limit=CONSTANT) public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
@Realtime(limit=LINEAR) public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
@Realtime(limit=CONSTANT) public V put(K key, V value)
@Realtime(limit=LINEAR) public void putAll(Map<? extends K,? extends V> map)
@Realtime(limit=CONSTANT) public V remove(Object key)
@Realtime(limit=CONSTANT) public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
@Realtime(limit=CONSTANT) public boolean remove(Object key, Object value)
remove
in interface ConcurrentMap<K,V>
@Realtime(limit=CONSTANT) public boolean replace(K key, V oldValue, V newValue)
replace
in interface ConcurrentMap<K,V>
@Realtime(limit=CONSTANT) public V replace(K key, V value)
replace
in interface ConcurrentMap<K,V>
public FastMap<K,V> putAll(FastMap<? extends K,? extends V> that)
public <T extends Map<K,V>> Immutable<T> toImmutable()
unmodifiable
view of this map
for which the caller guarantees that no change will ever be made
(e.g. there is no reference left to the original map).@Realtime(limit=LINEAR) public String toString()
protected MapService<K,V> service()
Copyright © 2005-2013 Javolution. All Rights Reserved.