Class SmoothieMap<K,V>
- java.lang.Object
-
- io.timeandspace.smoothie.SmoothieMap<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this mapV
- the type of mapped values
public class SmoothieMap<K,V> extends Object implements ObjObjMap<K,V>
UnorderedMap
with worstput
latencies more than 100 times smaller than in ordinary hash table implementations likeHashMap
and very low footprint per entry. SmoothieMap may also operate in the "low-garbage" or the "footprint" modes.SmoothieMap is created using a builder:
SmoothieMap.newBuilder().build()
. See possible configurations in the documentation forSmoothieMapBuilder
.Unlike
HashMap
, but likeConcurrentHashMap
,Map.of()
immutable Maps, and Guava's ImmutableMaps, SmoothieMap does not support null key and values. An attempt to put null key or value, or query null key or value (e. g. viaget(null)
), leads to aNullPointerException
.SmoothieMap
supports pluggable keys' and values' equivalences which could be configured in the builder, viaSmoothieMapBuilder.keyEquivalence(Equivalence)
andSmoothieMapBuilder.valueEquivalence(Equivalence)
methods.Functional additions to the
Map
interface implemented by SmoothieMap are described in the documentation forObjObjMap
interface. It also providessizeInBytes()
to track the footprint of the map.Note that this implementation is not synchronized. If multiple threads access a
SmoothieMap
concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map.If no such object exists, the map should be "wrapped" using the
Collections.synchronizedMap
method. This is best done at creation time, to prevent accidental unsynchronized access to the map:Map m = Collections.synchronizedMap(smoothieMap);
In terms of performance, favor calling bulk methods like
forEach(BiConsumer)
to iterating theSmoothieMap
viaIterator
, including for-each style iterations on map's collections views. Especially if you need to remove entries during iteration (i. e. callIterator.remove()
) and hash code for key objects is not cached on their side (like it is cached, for example, inString
class) - try to express your logic usingremoveIf(BiPredicate)
method in this case.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<K,V>
asMapWithMutableIterators()
void
clear()
Removes all of the mappings from this map.@Nullable V
compute(K key, BiFunction<? super K,? super @Nullable V,? extends @Nullable V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping).@Nullable V
computeIfAbsent(K key, Function<? super K,? extends @Nullable V> mappingFunction)
If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unlessnull
.@Nullable V
computeIfPresent(K key, BiFunction<? super K,? super V,? extends @Nullable V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.boolean
containsEntry(Object key, Object value)
Returnstrue
if this map contains a mapping of the given key and value.boolean
containsKey(Object key)
Returnstrue
if this map contains a mapping for the specified key.boolean
containsValue(Object value)
Returnstrue
if this map has one or more keys associated with the specified value.ObjSet<Map.Entry<K,V>>
entrySet()
Returns aSet
view of the mappings contained in this map.boolean
equals(Object obj)
Returns true if the other object is aMap
; this map and the given map have the same size; and for all entries in the other map,containsEntry(e.getKey(), e.getValue())
called on this map returns true.void
forEach(BiConsumer<? super K,? super V> action)
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.boolean
forEachWhile(BiPredicate<? super K,? super V> predicate)
Checks the givenpredicate
on each entry in this map until all entries have been processed or the predicate returns false for some entry, or throws an Exception.@Nullable V
get(Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.@Nullable K
getInternalKey(Object key)
Returns the key object held by this map internally and equivalent to the specified key, if there is one, ornull
if this map contains no mapping for the key.V
getOrDefault(Object key, V defaultValue)
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.int
hashCode()
Returns a sum of the following expressions:keyEquivalence().hash(key) ^ valueEquivalence().hash(value)
applied to all entries in this map.boolean
isEmpty()
Equivalence<K>
keyEquivalence()
Returns the equivalence strategy for keys for this map.ObjSet<K>
keySet()
Returns aSet
view of the keys contained in this map.@Nullable V
merge(K key, V value, BiFunction<? super V,? super V,? extends @Nullable V> remappingFunction)
If the specified key is not already associated with a value or is associated with null, associates it with the given value.Iterator<Map.Entry<K,V>>
mutableEntryIterator()
Returns an iterator over the entries in this SmoothieMap that supportsIterator.remove()
operation.Iterator<K>
mutableKeyIterator()
Returns an iterator over the keys in this SmoothieMap that supportsIterator.remove()
operation.Iterator<V>
mutableValueIterator()
Returns an iterator over the values in this SmoothieMap that supportsIterator.remove()
operation.static <K,V>
SmoothieMapBuilder<K,V>newBuilder()
Creates a newSmoothieMapBuilder
.@Nullable V
put(K key, V value)
Associates the specified value with the specified key in this map.void
putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.@Nullable V
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associates it with the given value and returnsnull
, else returns the current value.@Nullable V
remove(Object key)
Removes the mapping for a key from this map if it is present.boolean
remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.boolean
removeIf(BiPredicate<? super K,? super V> filter)
Removes all of the entries of this map that satisfy the given predicate.V
replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.boolean
replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.void
replaceAll(BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.int
size()
long
sizeAsLong()
Returns the number of entries in the map as along
value (not truncated toInteger.MAX_VALUE
, if the map size exceeds it, as returned by theMap.size()
method).long
sizeInBytes()
Returns the approximate footprint of thisSmoothieMap
instance in the heap of the JVM process, in bytes.String
toString()
Equivalence<V>
valueEquivalence()
Returns the equivalence strategy for values for this map.Collection<V>
values()
Returns aCollection
view of the values contained in this map.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.timeandspace.collect.map.ObjObjMap
mappingCount
-
-
-
-
Method Detail
-
newBuilder
@Contract(value=" -> new", pure=true) public static <K,V> SmoothieMapBuilder<K,V> newBuilder()
Creates a newSmoothieMapBuilder
.- Type Parameters:
K
- the type of keys in SmoothieMap(s) to be createdV
- the type of values in SmoothieMap(s) to be created- Returns:
- a new
SmoothieMapBuilder
-
sizeInBytes
public final long sizeInBytes()
Returns the approximate footprint of thisSmoothieMap
instance in the heap of the JVM process, in bytes. Does not include the footprints of the keys and values stored in theSmoothieMap
.- Returns:
- the approximate footprint of this
SmoothieMap
proper
-
keyEquivalence
public Equivalence<K> keyEquivalence()
Description copied from interface:ObjObjMap
Returns the equivalence strategy for keys for this map. All methods in theMap
interface which are defined in terms ofObject.equals(Object)
equality of key objects use this Equivalence instead (includingObjObjMap.equals(java.lang.Object)
,ObjObjMap.hashCode()
, andObjObjMap.keySet()
's andObjObjMap.entrySet()
'sequals()
andhashCode()
, but excludingequals()
andhashCode()
ofMap.Entry
objects which can be obtained from theObjObjMap.entrySet()
).- Specified by:
keyEquivalence
in interfaceObjObjMap<K,V>
- Returns:
- the equivalence strategy for keys for this map
-
valueEquivalence
public Equivalence<V> valueEquivalence()
Description copied from interface:ObjObjMap
Returns the equivalence strategy for values for this map. All methods in theMap
interface which defined in terms ofObject.equals(Object)
equality of value objects, such asObjObjMap.containsValue(Object)
andObjObjMap.remove(Object, Object)
use this Equivalence instead (includingObjObjMap.equals(java.lang.Object)
,ObjObjMap.hashCode()
, andObjObjMap.entrySet()
'sequals()
andhashCode()
, but excludingequals()
andhashCode()
ofMap.Entry
objects which can be obtained from theObjObjMap.entrySet()
).- Specified by:
valueEquivalence
in interfaceObjObjMap<K,V>
- Returns:
- the equivalence strategy for values for this map
-
sizeAsLong
public final long sizeAsLong()
Description copied from interface:ObjObjMap
Returns the number of entries in the map as along
value (not truncated toInteger.MAX_VALUE
, if the map size exceeds it, as returned by theMap.size()
method).- Specified by:
sizeAsLong
in interfaceObjObjMap<K,V>
- Returns:
- the number of key-value mappings in this map
- See Also:
Map.size()
,ObjObjMap.mappingCount()
-
containsKey
public final boolean containsKey(Object key)
Description copied from interface:ObjObjMap
Returnstrue
if this map contains a mapping for the specified key. More formally, returnstrue
if and only if this map contains a mapping for a keyk
such that the specifiedkey
andk
are equivalent. (There can be at most one such mapping.)- Specified by:
containsKey
in interfaceMap<K,V>
- Specified by:
containsKey
in interfaceObjObjMap<K,V>
- Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified key
-
containsEntry
public final boolean containsEntry(Object key, Object value)
Description copied from interface:ObjObjMap
Returnstrue
if this map contains a mapping of the given key and value. More formally, this map should contain a mapping from a keyk
to a valuev
such that the specifiedkey
andk
are equivalent with regard toObjObjMap.keyEquivalence()
, and the specifiedvalue
andv
are equivalent with regard toObjObjMap.valueEquivalence()
. (There can be at most one such mapping.)- Specified by:
containsEntry
in interfaceObjObjMap<K,V>
- Parameters:
key
- the key of the mapping to check presence ofvalue
- the value of the mapping to check presence of- Returns:
true
if this map contains the specified mapping,false
otherwise
-
getOrDefault
public final V getOrDefault(Object key, V defaultValue)
Description copied from interface:ObjObjMap
Returns the value to which the specified key is mapped, ordefaultValue
if this map contains no mapping for the key.- Specified by:
getOrDefault
in interfaceMap<K,V>
- Specified by:
getOrDefault
in interfaceObjObjMap<K,V>
- Parameters:
key
- the key whose associated value is to be returneddefaultValue
- the default mapping of the key- Returns:
- the value to which the specified key is mapped, or
defaultValue
if this map contains no mapping for the key
-
remove
@CanIgnoreReturnValue public final @Nullable V remove(Object key)
Description copied from interface:ObjObjMap
Removes the mapping for a key from this map if it is present. More formally, if this map contains a mapping from keyk
to valuev
such that the specifiedkey
andk
are equivalent, that mapping is removed. (The map can contain at most one such mapping.)Returns the value to which this map previously associated the key, or
null
if the map contained no mapping for the key.The map will not contain a mapping for the specified key once the call returns.
-
remove
public final boolean remove(Object key, Object value)
Description copied from interface:ObjObjMap
Removes the entry for the specified key only if it is currently mapped to the specified value. Values are compared usingObjObjMap.valueEquivalence()
.
-
replace
public final V replace(K key, V value)
Description copied from interface:ObjObjMap
Replaces the entry for the specified key only if it is currently mapped to some value.- Specified by:
replace
in interfaceMap<K,V>
- Specified by:
replace
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key.
-
replace
public final boolean replace(K key, V oldValue, V newValue)
Description copied from interface:ObjObjMap
Replaces the entry for the specified key only if currently mapped to the specified value. Values are compared usingObjObjMap.valueEquivalence()
.- Specified by:
replace
in interfaceMap<K,V>
- Specified by:
replace
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified key- Returns:
true
if the value was replaced
-
put
@CanIgnoreReturnValue public final @Nullable V put(K key, V value)
Description copied from interface:ObjObjMap
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A mapm
is said to contain a mapping for a keyk
if and only ifm.containsKey(k)
would returntrue
.)- Specified by:
put
in interfaceMap<K,V>
- Specified by:
put
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
-
putIfAbsent
public final @Nullable V putIfAbsent(K key, V value)
Description copied from interface:ObjObjMap
If the specified key is not already associated with a value, associates it with the given value and returnsnull
, else returns the current value.- Specified by:
putIfAbsent
in interfaceMap<K,V>
- Specified by:
putIfAbsent
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with the specified key, or
null
if there was no mapping for the key
-
get
public final @Nullable V get(Object key)
Description copied from interface:ObjObjMap
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.More formally, if this map contains a mapping from a key
k
to a valuev
such that the specifiedkey
andk
are equivalent, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)
-
getInternalKey
public final @Nullable K getInternalKey(Object key)
Description copied from interface:ObjObjMap
Returns the key object held by this map internally and equivalent to the specified key, if there is one, ornull
if this map contains no mapping for the key.This method could be used to deduplicate objects in the application, to reduce the memory footprint and make the application to conform to the "most objects die young" hypothesis that most GC algorithms are optimized for. This method is functionally similar to
String.intern()
and Guava's Interner, but allows to piggy-back a map data structure which may already exist in an application.ObjObjMap.keySet()
.getInternal(key)
delegates to this method.- Specified by:
getInternalKey
in interfaceObjObjMap<K,V>
- Parameters:
key
- the key whose equivalent held by this map internally is to be returned- Returns:
- the map-internal equivalent of the specified key, or
null
if the map contains no mapping for the specified key
-
computeIfPresent
public final @Nullable V computeIfPresent(K key, BiFunction<? super K,? super V,? extends @Nullable V> remappingFunction)
Description copied from interface:ObjObjMap
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.If the function returns
null
, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.- Specified by:
computeIfPresent
in interfaceMap<K,V>
- Specified by:
computeIfPresent
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedremappingFunction
- the function to compute a value- Returns:
- the new value associated with the specified key, or null if none
-
computeIfAbsent
public final @Nullable V computeIfAbsent(K key, Function<? super K,? extends @Nullable V> mappingFunction)
Description copied from interface:ObjObjMap
If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unlessnull
.If the function returns
null
no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:map.computeIfAbsent(key, k -> new Value(f(k)));
Or to implement a multi-value map,
Map<K,Collection<V>>
, supporting multiple values per key:map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
- Specified by:
computeIfAbsent
in interfaceMap<K,V>
- Specified by:
computeIfAbsent
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
-
compute
public final @Nullable V compute(K key, BiFunction<? super K,? super @Nullable V,? extends @Nullable V> remappingFunction)
Description copied from interface:ObjObjMap
Attempts to compute a mapping for the specified key and its current mapped value (ornull
if there is no current mapping). For example, to either create or append aString
msg to a value mapping:map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
merge()
is often simpler to use for such purposes.)If the function returns
null
, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
-
merge
public final @Nullable V merge(K key, V value, BiFunction<? super V,? super V,? extends @Nullable V> remappingFunction)
Description copied from interface:ObjObjMap
If the specified key is not already associated with a value or is associated with null, associates it with the given value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result isnull
. This method may be of use when combining multiple mapped values for a key. For example, to either create or append aString msg
to a value mapping:map.merge(key, msg, String::concat)
If the function returns
null
the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.- Specified by:
merge
in interfaceMap<K,V>
- Specified by:
merge
in interfaceObjObjMap<K,V>
- Parameters:
key
- key with which the resulting value is to be associatedvalue
- the value to be merged with the existing value associated with the key or, if no existing value is associated with the key, to be associated with the keyremappingFunction
- the function to recompute a value if present- Returns:
- the new value associated with the specified key, or null if no value is associated with the key
-
equals
public boolean equals(Object obj)
Description copied from interface:ObjObjMap
Returns true if the other object is aMap
; this map and the given map have the same size; and for all entries in the other map,containsEntry(e.getKey(), e.getValue())
called on this map returns true.Note that the specification above means that
equals()
depends onObjObjMap.keyEquivalence()
andObjObjMap.valueEquivalence()
of this map, if they are custom. This may be inconsistent with the generalMap.equals(java.lang.Object)
contract.
-
hashCode
public final int hashCode()
Description copied from interface:ObjObjMap
Returns a sum of the following expressions:keyEquivalence().hash(key) ^ valueEquivalence().hash(value)
applied to all entries in this map. Note that if this map has a customObjObjMap.keyEquivalence()
orObjObjMap.valueEquivalence()
, the resulting hash code may be inconsistent with the generalMap.hashCode()
contract.
-
forEach
public final void forEach(BiConsumer<? super K,? super V> action)
Description copied from interface:ObjObjMap
Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. Actions are performed in the order of entry set iteration. Exceptions thrown by the action are relayed to the caller.The entries will be processed in the same order as they appear the entry set's iterator and
ObjObjMap.forEachWhile(BiPredicate)
.
-
forEachWhile
public final boolean forEachWhile(BiPredicate<? super K,? super V> predicate)
Description copied from interface:ObjObjMap
Checks the givenpredicate
on each entry in this map until all entries have been processed or the predicate returns false for some entry, or throws an Exception. Exceptions thrown by the predicate are relayed to the caller.The entries will be processed in the same order as they appear in the entry set's iterator and
ObjObjMap.forEach(BiConsumer)
.If the map is empty, this method returns
true
immediately.- Specified by:
forEachWhile
in interfaceObjObjMap<K,V>
- Parameters:
predicate
- the predicate to be checked for each entry- Returns:
true
if the map is empty, or if the predicate returnedtrue
for all entries of the map,false
if the predicate returnedfalse
for some entry- See Also:
ObjObjMap.forEach(BiConsumer)
-
replaceAll
public final void replaceAll(BiFunction<? super K,? super V,? extends V> function)
Description copied from interface:ObjObjMap
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.- Specified by:
replaceAll
in interfaceMap<K,V>
- Specified by:
replaceAll
in interfaceObjObjMap<K,V>
- Parameters:
function
- the function to apply to each entry
-
containsValue
public final boolean containsValue(Object value)
Description copied from interface:ObjObjMap
Returnstrue
if this map has one or more keys associated with the specified value. More formally, returnstrue
if and only if this map contains at least one mapping to a valuev
such thatObjObjMap.valueEquivalence()
.equivalent(value, v) == true
. This operation requires time linear in the map size.- Specified by:
containsValue
in interfaceMap<K,V>
- Specified by:
containsValue
in interfaceObjObjMap<K,V>
- Parameters:
value
- value whose presence in this map is to be tested- Returns:
true
if this map maps one or more keys to the specified value
-
putAll
public final void putAll(Map<? extends K,? extends V> m)
Description copied from interface:ObjObjMap
Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from keyk
to valuev
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
-
clear
public final void clear()
Description copied from interface:ObjObjMap
Removes all of the mappings from this map. The map will be empty after this call returns.
-
removeIf
public final boolean removeIf(BiPredicate<? super K,? super V> filter)
Description copied from interface:ObjObjMap
Removes all of the entries of this map that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.Note the order in which this method visits entries may be different from the iteration and
ObjObjMap.forEach(BiConsumer)
order.
-
keySet
@EnsuresNonNull("keySet") public final ObjSet<K> keySet()
Returns aSet
view 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.If a structural modification of the map (new entry insertion or an entry removal) is detected while an iteration over the set is in progress (except through the iterator's own
remove
operation),ConcurrentModificationException
is thrown.The set supports element removal, which removes the corresponding mapping from the map, via the
Iterator.remove()
(optionally),Set.remove(java.lang.Object)
,Set.removeAll(java.util.Collection<?>)
,Set.retainAll(java.util.Collection<?>)
, andSet.clear()
operations.Set.remove(java.lang.Object)
,Set.contains(java.lang.Object)
, andSet.containsAll(java.util.Collection<?>)
operations on the returned set as well as key set's ownequals()
andhashCode()
respect the map'sObjObjMap.keyEquivalence()
. When this map has a customObjObjMap.keyEquivalence()
,equals()
on the key set works as follows: another object is considered equal to the key set if it is aSet
which has the same size as this map and for all elements in another set,keySet.contains(elementOfAnotherSet)
returns true.hashCode()
on the key set returns a sum of results of calls toEquivalence.hash(T)
on all elements in the key set.The key set does not support the
Set.add(E)
andSet.addAll(java.util.Collection<? extends E>)
operations.The set is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same set.
Calling
Set.iterator()
on the key set returns an iterator that may not supportIterator.remove()
. UseasMapWithMutableIterators()
.keySet()
ormutableKeyIterator()
methods instead if you need to remove entries while iterating a SmoothieMap.
-
mutableKeyIterator
public Iterator<K> mutableKeyIterator()
Returns an iterator over the keys in this SmoothieMap that supportsIterator.remove()
operation. This method may be usable because iterator ofkeySet()
may not supportIterator.remove()
.- Returns:
- a remove-supporting iterator over the keys in this SmoothieMap
-
values
@EnsuresNonNull("values") public final Collection<V> values()
Returns aCollection
view 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.If a structural modification of the map (new entry insertion or an entry removal) is detected while an iteration over the collection is in progress (except through the iterator's own
remove
operation),ConcurrentModificationException
is thrown.The collection supports element removal, which removes the corresponding mapping from the map, via the
Iterator.remove()
(optionally),Collection.remove(java.lang.Object)
,Collection.removeAll(java.util.Collection<?>)
,Collection.retainAll(java.util.Collection<?>)
andCollection.clear()
operations.Collection.remove(java.lang.Object)
,Collection.contains(java.lang.Object)
, andCollection.containsAll(java.util.Collection<?>)
on the returned collection respect the map'sObjObjMap.valueEquivalence()
.The values collection does not support the
add
oraddAll
operations.The collection is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same collection.
Calling
Collection.iterator()
on the values view returns an iterator that may not supportIterator.remove()
. UseasMapWithMutableIterators()
.values()
ormutableValueIterator()
methods instead if you need to remove entries while iterating a SmoothieMap.
-
mutableValueIterator
public Iterator<V> mutableValueIterator()
Returns an iterator over the values in this SmoothieMap that supportsIterator.remove()
operation. This method may be usable because iterator ofvalues()
may not supportIterator.remove()
.- Returns:
- a remove-supporting iterator over the values in this SmoothieMap
-
entrySet
@EnsuresNonNull("entrySet") public final ObjSet<Map.Entry<K,V>> entrySet()
Returns aSet
view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.If a structural modification of the map (new entry insertion or an entry removal) is detected while an iteration over the set is in progress (except through the iterator's own
remove
operation),ConcurrentModificationException
is thrown.The set supports element removal, which removes the corresponding mapping from the map, via the
Iterator.remove()
(optionally),Set.remove(java.lang.Object)
,Set.removeAll(java.util.Collection<?>)
,Set.retainAll(java.util.Collection<?>)
, andSet.clear()
operations.Set.remove(java.lang.Object)
,Set.contains(java.lang.Object)
, andSet.containsAll(java.util.Collection<?>)
operations on the returned set as well as entry set's ownequals()
andhashCode()
respect the map'sObjObjMap.keyEquivalence()
andObjObjMap.valueEquivalence()
, but the implementations ofequals()
andhashCode()
for theMap.Entry
objects which can be obtained from the set use built-in Java object equality and hash code for the map's keys and values.When this map has a custom
ObjObjMap.keyEquivalence()
orObjObjMap.valueEquivalence()
,equals()
on the entry set works as follows: another object is considered equal to the entry set if it is aSet
which has the same size as this map; all elements in another set areMap.Entry
objects; and for all the entries in another set,thisMap.containsEntry(entryFromAnotherSet.getKey(), entryFromAnotherSet.getValue())
returns true.hashCode()
on the entry set is the same as on the map itself, seeObjObjMap.hashCode()
.The entry set does not support the
Set.add(E)
andSet.addAll(java.util.Collection<? extends E>)
operations.The set is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same set.
Calling
Collection.iterator()
on the entry set returns an iterator that may not supportIterator.remove()
. UseasMapWithMutableIterators()
.entrySet()
ormutableEntryIterator()
methods instead if you need to remove entries while iterating a SmoothieMap.An entry set view returned from this method also doesn't support
ObjSet.getInternal(E)
operation.
-
mutableEntryIterator
public Iterator<Map.Entry<K,V>> mutableEntryIterator()
Returns an iterator over the entries in this SmoothieMap that supportsIterator.remove()
operation. This method may be usable because iterator ofentrySet()
may not supportIterator.remove()
.- Returns:
- a remove-supporting iterator over the entries in this SmoothieMap
-
-