SimpleArrayMap


public class SimpleArrayMap<K extends Object, V extends Object>

Known direct subclasses
ArrayMap

ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a traditional java.util.HashMap, this implementation is a version of the platform's android.util.ArrayMap that can be used on older versions of the platform.


Base implementation of androidx.collection.ArrayMap that doesn't include any standard Java container API interoperability. These features are generally heavier-weight ways to interact with the container, so discouraged, but they can be useful to make it easier to use as a drop-in replacement for HashMap. If you don't need them, this class can be preferable since it doesn't bring in any of the implementation of those APIs, allowing that code to be stripped by ProGuard.

NOTE: Consider using MutableScatterMap instead of this class. MutableScatterMap also avoids creating a new object per entry but offers better performance characteristics. If a Map interface is required, see MutableScatterMap.asMap. If a MutableMap interface is required, see MutableScatterMap.asMutableMap. A MutableScatterMap can also be passed as a ScatterMap to provide a read-only API surface.

Summary

Public constructors

<K extends Object, V extends Object> SimpleArrayMap(int capacity)

Create a new SimpleArrayMap with a given initial capacity.

<K extends Object, V extends Object> SimpleArrayMap(
    SimpleArrayMap<@NonNull K, @NonNull V> map
)

Create a new SimpleArrayMap with the mappings from the given map.

Public methods

void

Make the array map empty.

boolean

Check whether a key exists in the array.

boolean

Check whether a value exists in the array.

void
ensureCapacity(int minimumCapacity)

Ensure the array map can hold at least minimumCapacity items.

boolean
equals(Object other)

This implementation returns false if the object is not a Map or SimpleArrayMap, or if the maps have different sizes.

V
get(@NonNull K key)

Retrieve a value from the array.

@NonNull V
getOrDefault(Object key, @NonNull V defaultValue)

Retrieve a value from the array, or defaultValue if there is no mapping for the key.

int
int

Returns the index of a key in the set.

boolean

Return true if the array map contains no items.

@NonNull K
keyAt(int index)

Return the key at the given index in the array.

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

Add a new value to the array map.

void

Perform a put of all key/value pairs in map

V
putIfAbsent(@NonNull K key, @NonNull V value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

V
remove(@NonNull K key)

Remove an existing key from the array map.

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

Remove an existing key from the array map only if it is currently mapped to value.

@NonNull V
removeAt(int index)

Remove the key/value mapping at the given index.

V
replace(@NonNull K key, @NonNull V value)

Replace the mapping for key only if it is already mapped to a value.

boolean
replace(@NonNull K key, @NonNull V oldValue, @NonNull V newValue)

Replace the mapping for key only if it is already mapped to oldValue.

@NonNull V
setValueAt(int index, @NonNull V value)

Set the value at a given index in the array.

int

Return the number of items in this array map.

@NonNull String

Returns a string representation of the object.

@NonNull V
valueAt(int index)

Return the value at the given index in the array.

Public constructors

SimpleArrayMap

public <K extends Object, V extends Object> SimpleArrayMap(int capacity)

Create a new SimpleArrayMap with a given initial capacity. The default capacity of an array map is 0, and will grow once items are added to it.

SimpleArrayMap

public <K extends Object, V extends Object> SimpleArrayMap(
    SimpleArrayMap<@NonNull K, @NonNull V> map
)

Create a new SimpleArrayMap with the mappings from the given map.

Public methods

clear

Added in 1.0.0
public void clear()

Make the array map empty. All storage is released.

Throws
kotlin.ConcurrentModificationException

if it was detected that this SimpleArrayMap was written to while this operation was running.

containsKey

Added in 1.3.0
public boolean containsKey(@NonNull K key)

Check whether a key exists in the array.

Parameters
@NonNull K key

The key to search for.

Returns
boolean

Returns true if the key exists, else false.

containsValue

Added in 1.3.0
public boolean containsValue(@NonNull V value)

Check whether a value exists in the array. This requires a linear search through the entire array.

Parameters
@NonNull V value

The value to search for.

Returns
boolean

Returns true if the value exists, else false.

ensureCapacity

Added in 1.0.0
public void ensureCapacity(int minimumCapacity)

Ensure the array map can hold at least minimumCapacity items.

Throws
kotlin.ConcurrentModificationException

if it was detected that this SimpleArrayMap was written to while this operation was running.

equals

public boolean equals(Object other)

This implementation returns false if the object is not a Map or SimpleArrayMap, or if the maps have different sizes. Otherwise, for each key in this map, values of both maps are compared. If the values for any key are not equal, the method returns false, otherwise it returns true.

get

Added in 1.3.0
public V get(@NonNull K key)

Retrieve a value from the array.

Parameters
@NonNull K key

The key of the value to retrieve.

Returns
V

Returns the value associated with the given key, or null if there is no such key.

getOrDefault

Added in 1.1.0
public @NonNullgetOrDefault(Object key, @NonNull V defaultValue)

Retrieve a value from the array, or defaultValue if there is no mapping for the key.

Parameters
Object key

The key of the value to retrieve.

@NonNull V defaultValue

The default mapping of the key

Returns
@NonNull V

Returns the value associated with the given key, or defaultValue if there is no mapping for the key.

hashCode

public int hashCode()

indexOfKey

Added in 1.3.0
public int indexOfKey(@NonNull K key)

Returns the index of a key in the set.

Parameters
@NonNull K key

The key to search for.

Returns
int

Returns the index of the key if it exists, else a negative integer.

isEmpty

Added in 1.0.0
public boolean isEmpty()

Return true if the array map contains no items.

keyAt

Added in 1.0.0
public @NonNullkeyAt(int index)

Return the key at the given index in the array.

Parameters
int index

The desired index, must be between 0 and size-1 (inclusive).

Returns
@NonNull K

Returns the key stored at the given index.

Throws
kotlin.IllegalArgumentException

if index is not between 0 and size-1

put

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

Add a new value to the array map.

Parameters
@NonNull K key

The key under which to store the value. If this key already exists in the array, its value will be replaced.

@NonNull V value

The value to store for the given key.

Returns
V

Returns the old value that was stored for the given key, or null if there was no such key.

Throws
kotlin.ConcurrentModificationException

if it was detected that this SimpleArrayMap was written to while this operation was running.

putAll

Added in 1.0.0
public void putAll(@NonNull SimpleArrayMap<@NonNull K, @NonNull V> map)

Perform a put of all key/value pairs in map

Parameters
@NonNull SimpleArrayMap<@NonNull K, @NonNull V> map

The array whose contents are to be retrieved.

putIfAbsent

Added in 1.1.0
public V putIfAbsent(@NonNull K key, @NonNull V value)

Add a new value to the array map only if the key does not already have a value or it is mapped to null.

Parameters
@NonNull K key

The key under which to store the value.

@NonNull V value

The value to store for the given key.

Returns
V

Returns the value that was stored for the given key, or null if there was no such key.

remove

Added in 1.3.0
public V remove(@NonNull K key)

Remove an existing key from the array map.

Parameters
@NonNull K key

The key of the mapping to remove.

Returns
V

Returns the value that was stored under the key, or null if there was no such key.

remove

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

Remove an existing key from the array map only if it is currently mapped to value.

Parameters
@NonNull K key

The key of the mapping to remove.

@NonNull V value

The value expected to be mapped to the key.

Returns
boolean

Returns true if the mapping was removed.

removeAt

Added in 1.0.0
public @NonNullremoveAt(int index)

Remove the key/value mapping at the given index.

Parameters
int index

The desired index, must be between 0 and size-1 (inclusive).

Returns
@NonNull V

Returns the value that was stored at this index.

Throws
kotlin.ConcurrentModificationException

if it was detected that this SimpleArrayMap was written to while this operation was running.

kotlin.IllegalArgumentException

if index is not between 0 and size-1

replace

Added in 1.1.0
public V replace(@NonNull K key, @NonNull V value)

Replace the mapping for key only if it is already mapped to a value.

Parameters
@NonNull K key

The key of the mapping to replace.

@NonNull V value

The value to store for the given key.

Returns
V

Returns the previous mapped value or null.

replace

Added in 1.1.0
public boolean replace(@NonNull K key, @NonNull V oldValue, @NonNull V newValue)

Replace the mapping for key only if it is already mapped to oldValue.

Parameters
@NonNull K key

The key of the mapping to replace.

@NonNull V oldValue

The value expected to be mapped to the key.

@NonNull V newValue

The value to store for the given key.

Returns
boolean

Returns true if the value was replaced.

setValueAt

Added in 1.0.0
public @NonNullsetValueAt(int index, @NonNull V value)

Set the value at a given index in the array.

Parameters
int index

The desired index, must be between 0 and size-1 (inclusive).

@NonNull V value

The new value to store at this index.

Returns
@NonNull V

Returns the previous value at the given index.

Throws
kotlin.IllegalArgumentException

if index is not between 0 and size-1

size

Added in 1.0.0
public int size()

Return the number of items in this array map.

toString

public @NonNull String toString()

Returns a string representation of the object.

This implementation composes a string by iterating over its mappings. If this map contains itself as a key or a value, the string "(this Map)" will appear in its place.

valueAt

Added in 1.0.0
public @NonNullvalueAt(int index)

Return the value at the given index in the array.

Parameters
int index

The desired index, must be between 0 and size-1 (inclusive).

Returns
@NonNull V

Returns the value stored at the given index.

Throws
kotlin.IllegalArgumentException

if index is not between 0 and size-1