Class Overview
ArrayMap is a generic key->value mapping data structure that is
designed to be more memory efficient than a traditional HashMap
,
this implementation is a version of the platform's
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.
Summary
Public Constructors |
|
ArrayMap()
|
|
ArrayMap(int capacity)
Create a new ArrayMap with a given initial capacity.
|
|
ArrayMap(SimpleArrayMap map)
Create a new ArrayMap with the mappings from the given ArrayMap.
|
Public Methods |
boolean
|
containsAll(Collection<?> collection)
Determine if the array map contains all of the keys in the given collection.
|
Set<Entry<K, V>>
|
entrySet()
Return a Set for iterating over and interacting with all mappings
in the array map.
|
Set<K>
|
keySet()
Return a Set for iterating over and interacting with all keys
in the array map.
|
void
|
putAll(Map<? extends K, ? extends V> map)
|
boolean
|
removeAll(Collection<?> collection)
Remove all keys in the array map that exist in the given collection.
|
boolean
|
retainAll(Collection<?> collection)
Remove all keys in the array map that do not exist in the given collection.
|
Collection<V>
|
values()
Return a Collection for iterating over and interacting with all values
in the array map.
|
[Expand]
Inherited Methods |
From class
android.support.v4.util.SimpleArrayMap
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)
Compares this instance with the specified object and indicates if they
are equal.
This implementation returns false if the object is not a map, or
if the maps have different sizes.
|
V
|
get(Object key)
Retrieve a value from the array.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
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)
|
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.
|
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()
Returns a string containing a concise, human-readable description of this
object.
This implementation composes a string by iterating over its mappings.
|
V
|
valueAt(int index)
Return the value at the given index in the array.
|
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
|
From interface
java.util.Map
abstract
void
|
clear()
Removes all elements from this Map , leaving it empty.
|
abstract
boolean
|
containsKey(Object key)
Returns whether this Map contains the specified key.
|
abstract
boolean
|
containsValue(Object value)
Returns whether this Map contains the specified value.
|
abstract
Set<Entry<K, V>>
|
entrySet()
Returns a Set containing all of the mappings in this Map .
|
abstract
boolean
|
equals(Object object)
Compares the argument to the receiver, and returns true if the
specified object is a Map and both Map s contain the same mappings.
|
abstract
V
|
get(Object key)
Returns the value of the mapping with the specified key.
|
abstract
int
|
hashCode()
Returns an integer hash code for the receiver.
|
abstract
boolean
|
isEmpty()
Returns whether this map is empty.
|
abstract
Set<K>
|
keySet()
Returns a set of the keys contained in this Map .
|
abstract
V
|
put(K key, V value)
Maps the specified key to the specified value.
|
abstract
void
|
putAll(Map<? extends K, ? extends V> map)
Copies every mapping in the specified Map to this Map .
|
abstract
V
|
remove(Object key)
Removes a mapping with the specified key from this Map .
|
abstract
int
|
size()
Returns the number of mappings in this Map .
|
abstract
Collection<V>
|
values()
Returns a Collection of the values contained in this Map .
|
|
Public Constructors
public
ArrayMap
(int capacity)
Create a new ArrayMap with a given initial capacity.
Create a new ArrayMap with the mappings from the given ArrayMap.
Public Methods
public
boolean
containsAll
(Collection<?> collection)
Determine if the array map contains all of the keys in the given collection.
Parameters
collection
| The collection whose contents are to be checked against. |
Returns
- Returns true if this array map contains a key for every entry
in collection, else returns false.
public
Set<Entry<K, V>>
entrySet
()
Return a 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
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
.
public
Set<K>
keySet
()
Return a 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.
public
void
putAll
(Map<? extends K, ? extends V> map)
Parameters
map
| The map whose contents are to be retrieved.
|
public
boolean
removeAll
(Collection<?> collection)
Remove all keys in the array map that exist in the given collection.
Parameters
collection
| The collection whose contents are to be used to remove keys. |
Returns
- Returns true if any keys were removed from the array map, else false.
public
boolean
retainAll
(Collection<?> collection)
Remove all keys in the array map that do not exist in the given collection.
Parameters
collection
| The collection whose contents are to be used to determine which
keys to keep. |
Returns
- Returns true if any keys were removed from the array map, else false.
Return a 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.
Returns
- a collection of the values contained in this map.