Register now for Android Dev Summit 2019!

SimpleArrayMap

public class SimpleArrayMap
extends Object

java.lang.Object
   ↳ androidx.collection.SimpleArrayMap<K, V>


Base implementation of 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 preferrable since it doesn't bring in any of the implementation of those APIs, allowing that code to be stripped by ProGuard.

Summary

Public constructors

SimpleArrayMap()

Create a new empty ArrayMap.

SimpleArrayMap(int capacity)

Create a new ArrayMap with a given initial capacity.

SimpleArrayMap(SimpleArrayMap<K, V> map)

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

Public methods

void clear()

Make the array map empty.

boolean containsKey(Object key)

Check whether a key exists in the array.

boolean containsValue(Object value)

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 object)

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

V get(Object key)

Retrieve a value from the array.

V getOrDefault(Object key, V defaultValue)

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

int hashCode()

int indexOfKey(Object key)

Returns the index of a key in the set.

boolean isEmpty()

Return true if the array map contains no items.

K keyAt(int index)

Return the key at the given index in the array.

V put(K key, V value)

Add a new value to the array map.

void putAll(SimpleArrayMap<? extends K, ? extends V> array)

Perform a put(Object, Object) of all key/value pairs in array

V putIfAbsent(K key, 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.

boolean remove(Object key, Object value)

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

V remove(Object key)

Remove an existing key from the array map.

V removeAt(int index)

Remove the key/value mapping at the given index.

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

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

V replace(K key, V value)

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

V setValueAt(int index, V value)

Set the value at a given index in the array.

int size()

Return the number of items in this array map.

String toString()

This implementation composes a string by iterating over its mappings.

V valueAt(int index)

Return the value at the given index in the array.

Inherited methods

Public constructors

SimpleArrayMap

public SimpleArrayMap ()

Create a new empty ArrayMap. The default capacity of an array map is 0, and will grow once items are added to it.

SimpleArrayMap

public SimpleArrayMap (int capacity)

Create a new ArrayMap with a given initial capacity.

Parameters
capacity int

SimpleArrayMap

public SimpleArrayMap (SimpleArrayMap<K, V> map)

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

Parameters
map SimpleArrayMap

Public methods

clear

public void clear ()

Make the array map empty. All storage is released.

containsKey

public boolean containsKey (Object key)

Check whether a key exists in the array.

Parameters
key Object: The key to search for.

Returns
boolean Returns true if the key exists, else false.

containsValue

public boolean containsValue (Object value)

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

Parameters
value Object: The value to search for.

Returns
boolean Returns true if the value exists, else false.

ensureCapacity

public void ensureCapacity (int minimumCapacity)

Ensure the array map can hold at least minimumCapacity items.

Parameters
minimumCapacity int

equals

public boolean equals (Object object)

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.

Parameters
object Object

Returns
boolean

get

public V get (Object key)

Retrieve a value from the array.

Parameters
key Object: 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

public V getOrDefault (Object key, 
                V defaultValue)

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

Parameters
key Object: The key of the value to retrieve.

defaultValue V: The default mapping of the key

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

hashCode

public int hashCode ()

Returns
int

indexOfKey

public int indexOfKey (Object key)

Returns the index of a key in the set.

Parameters
key Object: The key to search for.

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

isEmpty

public boolean isEmpty ()

Return true if the array map contains no items.

Returns
boolean

keyAt

public K keyAt (int index)

Return the key at the given index in the array.

Parameters
index int: The desired index, must be between 0 and size()-1.

Returns
K Returns the key stored at the given index.

put

public V put (K key, 
                V value)

Add a new value to the array map.

Parameters
key K: The key under which to store the value. Must not be null. If this key already exists in the array, its value will be replaced.

value V: 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.

putAll

public void putAll (SimpleArrayMap<? extends K, ? extends V> array)

Perform a put(Object, Object) of all key/value pairs in array

Parameters
array SimpleArrayMap: The array whose contents are to be retrieved.

putIfAbsent

public V putIfAbsent (K key, 
                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
key K: The key under which to store the value.

value V: 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

public boolean remove (Object key, 
                Object value)

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

Parameters
key Object: The key of the mapping to remove.

value Object: The value expected to be mapped to the key.

Returns
boolean Returns true if the mapping was removed.

remove

public V remove (Object key)

Remove an existing key from the array map.

Parameters
key Object: 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.

removeAt

public V removeAt (int index)

Remove the key/value mapping at the given index.

Parameters
index int: The desired index, must be between 0 and size()-1.

Returns
V Returns the value that was stored at this index.

replace

public boolean replace (K key, 
                V oldValue, 
                V newValue)

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

Parameters
key K: The key of the mapping to replace.

oldValue V: The value expected to be mapped to the key.

newValue V: The value to store for the given key.

Returns
boolean Returns true if the value was replaced.

replace

public V replace (K key, 
                V value)

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

Parameters
key K: The key of the mapping to replace.

value V: The value to store for the given key.

Returns
V Returns the previous mapped value or null.

setValueAt

public V setValueAt (int index, 
                V value)

Set the value at a given index in the array.

Parameters
index int: The desired index, must be between 0 and size()-1.

value V: The new value to store at this index.

Returns
V Returns the previous value at the given index.

size

public int size ()

Return the number of items in this array map.

Returns
int

toString

public String toString ()

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.

Returns
String

valueAt

public V valueAt (int index)

Return the value at the given index in the array.

Parameters
index int: The desired index, must be between 0 and size()-1.

Returns
V Returns the value stored at the given index.