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. It keeps its mappings in an array data structure -- an integer array of hash codes for each item, and an Object array of the key/value pairs. This allows it to avoid having to create an extra object for every entry put in to the map, and it also tries to control the growth of the size of these arrays more aggressively (since growing them only requires copying the entries in the array, not rebuilding a hash map).

If you don't need the standard Java container APIs provided here (iterators etc), consider using SimpleArrayMap instead.

Note that this implementation is not intended to be appropriate for data structures that may contain large numbers of items. It is generally slower than a traditional HashMap, since lookups require a binary search and adds and removes require inserting and deleting entries in the array. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

Because this container is intended to better balance memory use, unlike most other standard Java containers it will shrink its array as items are removed from it. Currently you have no control over this shrinking -- if you set a capacity and then remove an item, it may reduce the capacity to better match the current size. In the future an explicit call to set the capacity should turn off this aggressive shrinking behavior.

NOTE: Consider using MutableScatterMap instead of this class. MutableScatterMap also avoids creating an extra object per entry but offers overall better performance characteristics. If a Map-like interface is required, please see asMap and asMutableMap.

Summary

Public constructors

android
ArrayMap(capacity: Int)

Create a new ArrayMap with a given initial capacity.

android

Create a new ArrayMap with the mappings from the given ArrayMap.

android

Public functions

Boolean

Determine if the array map contains all of the keys in the given collection.

android
Boolean

Check whether a key exists in the array.

android
Boolean
containsValue(value: Any?)

Check whether a value exists in the array.

android
(Mutable)Set<(Mutable)Map.Entry<K!, V!>!>

Return a java.util.Set for iterating over and interacting with all mappings in the array map.

android
V!
get(key: Any?)

Retrieve a value from the array.

android
(Mutable)Set<K!>

Return a java.util.Set for iterating over and interacting with all keys in the array map.

android
Unit
putAll(map: (Mutable)Map<K!, V!>)

Perform a put of all key/value pairs in map

android
V!
remove(key: Any?)

Remove an existing key from the array map.

android
Boolean
removeAll(collection: (Mutable)Collection<Any!>)

Remove all keys in the array map that exist in the given collection.

android
Boolean
retainAll(collection: (Mutable)Collection<Any!>)

Remove all keys in the array map that do not exist in the given collection.

android
(Mutable)Collection<V!>

Return a java.util.Collection for iterating over and interacting with all values in the array map.

android

Inherited functions

From java.util.Map
abstract Unit
android
V!
compute(key: K!, remappingFunction: BiFunction<Any!, Any!, V!>!)
android
V!
computeIfAbsent(key: K!, mappingFunction: Function<Any!, V!>!)
android
V!
computeIfPresent(key: K!, remappingFunction: BiFunction<Any!, Any!, V!>!)
android
java-static (Mutable)Map<K!, V!>!
<K, V> copyOf(map: (Mutable)Map<K!, V!>!)
android
java-static (Mutable)Map.Entry<K!, V!>!
<K, V> entry(k: K!, v: V!)
android
abstract Boolean
equals(p: Any!)
android
Unit
forEach(action: BiConsumer<Any!, Any!>!)
android
V!
getOrDefault(key: Any!, defaultValue: V!)
android
abstract Int
android
abstract Boolean
android
V!
merge(key: K!, value: V!, remappingFunction: BiFunction<Any!, Any!, V!>!)
android
java-static (Mutable)Map<K!, V!>!
<K, V> of()
android
java-static (Mutable)Map<K!, V!>!
<K, V> ofEntries(entries: Array<(Mutable)Map.Entry<K!, V!>!>!)
android
abstract V!
put(p: K!, p1: V!)
android
V!
putIfAbsent(key: K!, value: V!)
android
Boolean
remove(key: Any!, value: Any!)
android
V!
replace(key: K!, value: V!)
android
Boolean
replace(key: K!, oldValue: V!, newValue: V!)
android
Unit
replaceAll(function: BiFunction<Any!, Any!, V!>!)
android
abstract Int
android
From androidx.collection.SimpleArrayMap
Int
android
Unit

Make the array map empty.

android
Unit
ensureCapacity(minimumCapacity: Int)

Ensure the array map can hold at least minimumCapacity items.

android
Boolean
equals(other: Any!)

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

android
V!
getOrDefault(key: Any!, defaultValue: V!)

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

android
Int
android
Int
indexOfKey(key: K!)

Returns the index of a key in the set.

android
Boolean

Return true if the array map contains no items.

android
K!
keyAt(index: Int)

Return the key at the given index in the array.

android
V!
put(key: K!, value: V!)

Add a new value to the array map.

android
Unit
putAll(map: SimpleArrayMap<K!, V!>!)

Perform a put of all key/value pairs in map

android
V!
putIfAbsent(key: K!, value: V!)

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

android
Boolean
remove(key: K!, value: V!)

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

android
V!
removeAt(index: Int)

Remove the key/value mapping at the given index.

android
V!
replace(key: K!, value: V!)

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

android
Boolean
replace(key: K!, oldValue: V!, newValue: V!)

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

android
V!
setValueAt(index: Int, value: V!)

Set the value at a given index in the array.

android
Int

Return the number of items in this array map.

android
String!

Returns a string representation of the object.

android
V!
valueAt(index: Int)

Return the value at the given index in the array.

android

Public constructors

ArrayMap

ArrayMap()

ArrayMap

ArrayMap(capacity: Int)

Create a new ArrayMap with a given initial capacity.

ArrayMap

ArrayMap(map: SimpleArrayMap?)

Create a new ArrayMap with the mappings from the given ArrayMap.

Public functions

containsAll

fun containsAll(collection: (Mutable)Collection<Any!>): Boolean

Determine if the array map contains all of the keys in the given collection.

Parameters
collection: (Mutable)Collection<Any!>

The collection whose contents are to be checked against.

Returns
Boolean

Returns true if this array map contains a key for every entry in collection, else returns false.

containsKey

fun containsKey(key: Any?): Boolean

Check whether a key exists in the array.

Parameters
key: Any?

The key to search for. ** This must be the same type as K **

Returns
Boolean

Returns true if the key exists, else false.

containsValue

fun containsValue(value: Any?): Boolean

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

Parameters
value: Any?

The value to search for. ** This must be the same type as V **

Returns
Boolean

Returns true if the value exists, else false.

entrySet

fun entrySet(): (Mutable)Set<(Mutable)Map.Entry<K!, V!>!>

Return a java.util.Set for iterating over and interacting with all mappings in the array map.

Note: this is a very inefficient way to access the array contents, it requires generating a number of temporary objects.

Note:

the semantics of this Set are subtly different than that of a java.util.HashMap: most important, the Map.Entry object returned by its iterator is a single object that exists for the entire iterator, so you can not hold on to it after calling Iterator.next.

get

fun get(key: Any?): V!

Retrieve a value from the array.

Parameters
key: Any?

The key of the value to retrieve. ** This must be the same type as K **

Returns
V!

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

keySet

fun keySet(): (Mutable)Set<K!>

Return a java.util.Set for iterating over and interacting with all keys in the array map.

Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects.

putAll

fun putAll(map: (Mutable)Map<K!, V!>): Unit

Perform a put of all key/value pairs in map

Parameters
map: (Mutable)Map<K!, V!>

The map whose contents are to be retrieved.

remove

fun remove(key: Any?): V!

Remove an existing key from the array map.

Parameters
key: Any?

The key of the mapping to remove. ** This must be the same type as V **

Returns
V!

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

removeAll

fun removeAll(collection: (Mutable)Collection<Any!>): Boolean

Remove all keys in the array map that exist in the given collection.

Parameters
collection: (Mutable)Collection<Any!>

The collection whose contents are to be used to remove keys.

Returns
Boolean

Returns true if any keys were removed from the array map, else false.

retainAll

fun retainAll(collection: (Mutable)Collection<Any!>): Boolean

Remove all keys in the array map that do not exist in the given collection.

Parameters
collection: (Mutable)Collection<Any!>

The collection whose contents are to be used to determine which keys to keep.

Returns
Boolean

Returns true if any keys were removed from the array map, else false.

values

fun values(): (Mutable)Collection<V!>

Return a java.util.Collection for iterating over and interacting with all values in the array map.

Note: this is a fairly inefficient way to access the array contents, it requires generating a number of temporary objects.