SimpleArrayMap
public class SimpleArrayMap<K extends Object, V extends Object>
ArrayMap |
ArrayMap is a generic key->value mapping data structure that is designed to be more memory efficient than a traditional |
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 |
<K extends Object, V extends Object> SimpleArrayMap(Create a new |
Public methods |
|
|---|---|
void |
clear()Make the array map empty. |
boolean |
containsKey(@NonNull K key)Check whether a key exists in the array. |
boolean |
containsValue(@NonNull V value)Check whether a value exists in the array. |
void |
ensureCapacity(int minimumCapacity)Ensure the array map can hold at least |
boolean |
This implementation returns |
V |
Retrieve a value from the array. |
@NonNull V |
getOrDefault(Object key, @NonNull V defaultValue)Retrieve a value from the array, or |
int |
hashCode() |
int |
indexOfKey(@NonNull K key)Returns the index of a key in the set. |
boolean |
isEmpty()Return |
@NonNull K |
keyAt(int index)Return the key at the given index in the array. |
V |
Add a new value to the array map. |
void |
putAll(@NonNull SimpleArrayMap<@NonNull K, @NonNull V> 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 |
V |
Remove an existing key from the array map. |
boolean |
Remove an existing key from the array map only if it is currently mapped to |
@NonNull V |
removeAt(int index)Remove the key/value mapping at the given index. |
V |
Replace the mapping for |
boolean |
Replace the mapping for |
@NonNull V |
setValueAt(int index, @NonNull V value)Set the value at a given index in the array. |
int |
size()Return the number of items in this array map. |
@NonNull String |
toString()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
public void clear()
Make the array map empty. All storage is released.
| Throws | |
|---|---|
kotlin.ConcurrentModificationException |
if it was detected that this |
containsKey
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 |
containsValue
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 |
ensureCapacity
public void ensureCapacity(int minimumCapacity)
Ensure the array map can hold at least minimumCapacity items.
| Throws | |
|---|---|
kotlin.ConcurrentModificationException |
if it was detected that this |
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
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 |
getOrDefault
public @NonNull V getOrDefault(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 |
indexOfKey
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. |
keyAt
public @NonNull K keyAt(int index)
Return the key at the given index in the array.
| Parameters | |
|---|---|
int index |
The desired index, must be between 0 and |
| Returns | |
|---|---|
@NonNull K |
Returns the key stored at the given index. |
put
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 |
| Throws | |
|---|---|
kotlin.ConcurrentModificationException |
if it was detected that this |
putAll
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
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 |
remove
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 |
remove
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 |
removeAt
public @NonNull V removeAt(int index)
Remove the key/value mapping at the given index.
| Parameters | |
|---|---|
int index |
The desired index, must be between 0 and |
| Returns | |
|---|---|
@NonNull V |
Returns the value that was stored at this index. |
| Throws | |
|---|---|
kotlin.ConcurrentModificationException |
if it was detected that this |
kotlin.IllegalArgumentException |
replace
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 |
replace
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 |
setValueAt
public @NonNull V setValueAt(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 |
@NonNull V value |
The new value to store at this index. |
| Returns | |
|---|---|
@NonNull V |
Returns the previous value at the given index. |
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.