MutableScatterMap


public final class MutableScatterMap<K extends Object, V extends Object> extends ScatterMap


MutableScatterMap is a container with a Map-like interface based on a flat hash table implementation (the key/value mappings are not stored by nodes but directly into arrays). The underlying implementation is designed to avoid all allocations on insertion, removal, retrieval, and iteration. Allocations may still happen on insertion when the underlying storage needs to grow to accommodate newly added entries to the table. In addition, this implementation minimizes memory usage by avoiding the use of separate objects to hold key/value pairs.

This implementation makes no guarantee as to the order of the keys and values stored, nor does it make guarantees that the order remains constant over time.

This implementation is not thread-safe: if multiple threads access this container concurrently, and one or more threads modify the structure of the map (insertion or removal for instance), the calling code must provide the appropriate synchronization. Multiple threads are safe to read from this map concurrently if no write is happening.

Note: when a Map is absolutely necessary, you can use the method asMap to create a thin wrapper around a MutableScatterMap. Please refer to asMap for more details and caveats.

Note: when a MutableMap is absolutely necessary, you can use the method asMutableMap to create a thin wrapper around a MutableScatterMap. Please refer to asMutableMap for more details and caveats.

MutableScatterMap and SimpleArrayMap: like SimpleArrayMap, MutableScatterMap is designed to avoid the allocation of extra objects when inserting new entries in the map. However, the implementation of MutableScatterMap offers better performance characteristics compared to SimpleArrayMap and is thus generally preferable. If memory usage is a concern, SimpleArrayMap automatically shrinks its storage to avoid using more memory than necessary. You can also control memory usage with MutableScatterMap by manually calling MutableScatterMap.trim.

See also
Map

Summary

Public constructors

<K extends Object, V extends Object> MutableScatterMap(int initialCapacity)

Creates a new MutableScatterMap

Public methods

final @NonNull Map<@NonNull K, @NonNull V>

Wraps this ScatterMap with a MutableMap interface.

final void

Removes all mappings from this map.

final @NonNull V
compute(
    @NonNull K key,
    @NonNull Function2<@NonNull key, value, @NonNull V> computeBlock
)

Retrieves a value for key and computes a new value based on the existing value (or null if the key is not in the map).

final @NonNull V
getOrPut(@NonNull K key, @NonNull Function0<@NonNull V> defaultValue)

Returns the value to which the specified key is mapped, if the value is present in the map and not null.

final void

Removes the specified key and its associated value from the map.

final void
minusAssign(@NonNull K[] keys)

Removes the specified keys and their associated value from the map.

final void

Removes the specified keys and their associated value from the map.

final void

Removes the specified keys and their associated value from the map.

final void

Removes the specified keys and their associated value from the map.

final void

Removes the specified keys and their associated value from the map.

final void

Puts all the key/value mappings in the from map into this map.

final void

Puts all the key/value mappings in the from map into this map.

final void

Puts the key/value mapping from the pair in this map, using the first element as the key, and the second element as the value.

final void
plusAssign(@NonNull Pair[] pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final void

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final void

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final V
put(@NonNull K key, @NonNull V value)

Creates a new mapping from key to value in this map.

final void
putAll(@NonNull Map<@NonNull K, @NonNull V> from)

Puts all the key/value mappings in the from map into this map.

final void

Puts all the key/value mappings in the from map into this map.

final void
putAll(@NonNull Pair[] pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final void

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final void

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

final V
remove(@NonNull K key)

Removes the specified key and its associated value from the map.

final boolean
remove(@NonNull K key, @NonNull V value)

Removes the specified key and its associated value from the map if the associated value equals value.

final void
removeIf(
    @NonNull Function2<@NonNull K, @NonNull V, @NonNull Boolean> predicate
)

Removes any mapping for which the specified predicate returns true.

final void
set(@NonNull K key, @NonNull V value)

Creates a new mapping from key to value in this map.

final int

Trims this MutableScatterMap's storage so it is sized appropriately to hold the current mappings.

Inherited methods

From androidx.collection.ScatterMap
final boolean
all(@NonNull Function2<@NonNull K, @NonNull V, @NonNull Boolean> predicate)

Returns true if all entries match the given predicate.

final boolean
any()

Returns true if this map has at least one entry.

final boolean
any(@NonNull Function2<@NonNull K, @NonNull V, @NonNull Boolean> predicate)

Returns true if at least one entry matches the given predicate.

final @NonNull Map<@NonNull K, @NonNull V>

Wraps this ScatterMap with a Map interface.

final boolean
contains(@NonNull K key)

Returns true if the specified key is present in this hash map, false otherwise.

final boolean

Returns true if the specified key is present in this hash map, false otherwise.

final boolean

Returns true if the specified value is present in this hash map, false otherwise.

final int

Returns the number of entries in this map.

final int
count(@NonNull Function2<@NonNull K, @NonNull V, @NonNull Boolean> predicate)

Returns the number of entries matching the given predicate.

boolean
equals(Object other)

Compares the specified object other with this hash map for equality.

final void
forEach(@NonNull Function2<@NonNull key, @NonNull value, Unit> block)

Iterates over every key/value pair stored in this map by invoking the specified block lambda.

final void
forEachKey(@NonNull Function1<@NonNull key, Unit> block)

Iterates over every key stored in this map by invoking the specified block lambda.

final void
forEachValue(@NonNull Function1<@NonNull value, Unit> block)

Iterates over every value stored in this map by invoking the specified block lambda.

final V
get(@NonNull K key)

Returns the value corresponding to the given key, or null if such a key is not present in the map.

final int

Returns the number of key-value pairs that can be stored in this map without requiring internal storage reallocation.

final @NonNull V
getOrDefault(@NonNull K key, @NonNull V defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

final @NonNull V
getOrElse(@NonNull K key, @NonNull Function0<@NonNull V> defaultValue)

Returns the value for the given key if the value is present and not null.

final int

Returns the number of key-value pairs in this map.

int

Returns the hash code value for this map.

final boolean

Indicates whether this map is empty.

final boolean

Returns true if this map is not empty.

final @NonNull String
joinToString(
    @NonNull CharSequence separator,
    @NonNull CharSequence prefix,
    @NonNull CharSequence postfix,
    int limit,
    @NonNull CharSequence truncated,
    Function2<@NonNull key, @NonNull value, @NonNull CharSequence> transform
)

Creates a String from the elements separated by separator and using prefix before and postfix after, if supplied.

final boolean

Returns true if this map has no entries.

@NonNull String

Returns a string representation of this map.

Public constructors

MutableScatterMap

public <K extends Object, V extends Object> MutableScatterMap(int initialCapacity)

Creates a new MutableScatterMap

Parameters
int initialCapacity

The initial desired capacity for this container. The container will honor this value by guaranteeing its internal structures can hold that many entries without requiring any allocations. The initial capacity can be set to 0.

Public methods

asMutableMap

Added in 1.4.0
public final @NonNull Map<@NonNull K, @NonNull V> asMutableMap()

Wraps this ScatterMap with a MutableMap interface. The MutableMap is backed by the ScatterMap, so changes to the ScatterMap are reflected in the MutableMap and vice-versa. If the ScatterMap is modified while an iteration over the MutableMap is in progress (and vice- versa), the results of the iteration are undefined.

Note: while this method is useful to use this MutableScatterMap with APIs accepting MutableMap interfaces, it is less efficient to do so than to use MutableScatterMap's APIs directly. While the MutableMap implementation returned by this method tries to be as efficient as possible, the semantics of MutableMap may require the allocation of temporary objects for access and iteration.

clear

Added in 1.4.0
public final void clear()

Removes all mappings from this map.

compute

Added in 1.4.0
public final @NonNullcompute(
    @NonNull K key,
    @NonNull Function2<@NonNull key, value, @NonNull V> computeBlock
)

Retrieves a value for key and computes a new value based on the existing value (or null if the key is not in the map). The computed value is then stored in the map for the given key.

Returns
@NonNull V

value computed by computeBlock.

getOrPut

Added in 1.4.0
public final @NonNullgetOrPut(@NonNull K key, @NonNull Function0<@NonNull V> defaultValue)

Returns the value to which the specified key is mapped, if the value is present in the map and not null. Otherwise, calls defaultValue() and puts the result in the map associated with key.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull K key)

Removes the specified key and its associated value from the map.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull K[] keys)

Removes the specified keys and their associated value from the map.

minusAssign

public final void minusAssign(@NonNull Iterable<@NonNull K> keys)

Removes the specified keys and their associated value from the map.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull ObjectList<@NonNull K> keys)

Removes the specified keys and their associated value from the map.

minusAssign

Added in 1.4.0
public final void minusAssign(@NonNull ScatterSet<@NonNull K> keys)

Removes the specified keys and their associated value from the map.

minusAssign

public final void minusAssign(@NonNull Sequence<@NonNull K> keys)

Removes the specified keys and their associated value from the map.

plusAssign

public final void plusAssign(@NonNull Map<@NonNull K, @NonNull V> from)

Puts all the key/value mappings in the from map into this map.

plusAssign

Added in 1.4.0
public final void plusAssign(@NonNull ScatterMap<@NonNull K, @NonNull V> from)

Puts all the key/value mappings in the from map into this map.

plusAssign

public final void plusAssign(@NonNull Pair<@NonNull K, @NonNull V> pair)

Puts the key/value mapping from the pair in this map, using the first element as the key, and the second element as the value.

plusAssign

public final void plusAssign(@NonNull Pair[] pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

plusAssign

public final void plusAssign(@NonNull Iterable<@NonNull Pair<@NonNull K, @NonNull V>> pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

plusAssign

public final void plusAssign(@NonNull Sequence<@NonNull Pair<@NonNull K, @NonNull V>> pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

put

Added in 1.4.0
public final V put(@NonNull K key, @NonNull V value)

Creates a new mapping from key to value in this map. If key is already present in the map, the association is modified and the previously associated value is replaced with value. If key is not present, a new entry is added to the map, which may require to grow the underlying storage and cause allocations. Return the previous value associated with the key, or null if the key was not present in the map.

putAll

public final void putAll(@NonNull Map<@NonNull K, @NonNull V> from)

Puts all the key/value mappings in the from map into this map.

putAll

Added in 1.4.0
public final void putAll(@NonNull ScatterMap<@NonNull K, @NonNull V> from)

Puts all the key/value mappings in the from map into this map.

putAll

public final void putAll(@NonNull Pair[] pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

putAll

public final void putAll(@NonNull Iterable<@NonNull Pair<@NonNull K, @NonNull V>> pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

putAll

public final void putAll(@NonNull Sequence<@NonNull Pair<@NonNull K, @NonNull V>> pairs)

Puts all the pairs into this map, using the first component of the pair as the key, and the second component as the value.

remove

Added in 1.4.0
public final V remove(@NonNull K key)

Removes the specified key and its associated value from the map. If the key was present in the map, this function returns the value that was present before removal.

remove

Added in 1.4.0
public final boolean remove(@NonNull K key, @NonNull V value)

Removes the specified key and its associated value from the map if the associated value equals value. Returns whether the removal happened.

removeIf

Added in 1.4.0
public final void removeIf(
    @NonNull Function2<@NonNull K, @NonNull V, @NonNull Boolean> predicate
)

Removes any mapping for which the specified predicate returns true.

set

Added in 1.4.0
public final void set(@NonNull K key, @NonNull V value)

Creates a new mapping from key to value in this map. If key is already present in the map, the association is modified and the previously associated value is replaced with value. If key is not present, a new entry is added to the map, which may require to grow the underlying storage and cause allocations.

trim

Added in 1.4.0
public final int trim()

Trims this MutableScatterMap's storage so it is sized appropriately to hold the current mappings.

Returns the number of empty entries removed from this map's storage. Returns be 0 if no trimming is necessary or possible.