|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractMap<K,V>
com.google.api.client.util.ArrayMap<K,V>
K
- the type of keys maintained by this mapV
- the type of mapped valuespublic class ArrayMap<K,V>
Memory-efficient map of keys to values with list-style random-access semantics.
Supports null keys and values. Conceptually, the keys and values are stored in a simpler array in
order to minimize memory use and provide for fast access to a key/value at a certain index (for
example getKey(int)
). However, traditional mapping operations like get(Object)
and put(Object, Object)
are slower because they need to look up all key/value pairs in
the worst case.
Implementation is not thread-safe. For a thread-safe choice instead use an implementation of
ConcurrentMap
.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.util.AbstractMap |
---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
Constructor Summary | |
---|---|
ArrayMap()
|
Method Summary | ||
---|---|---|
void |
add(K key,
V value)
Adds the key/value mapping at the end of the list. |
|
void |
clear()
|
|
ArrayMap<K,V> |
clone()
|
|
boolean |
containsKey(Object key)
Returns whether there is a mapping for the given key. |
|
boolean |
containsValue(Object value)
|
|
static
|
create()
Returns a new instance of an array map with initial capacity of zero. |
|
static
|
create(int initialCapacity)
Returns a new instance of an array map of the given initial capacity. |
|
void |
ensureCapacity(int minCapacity)
Ensures that the capacity of the internal arrays is at least a given capacity. |
|
Set<Map.Entry<K,V>> |
entrySet()
|
|
V |
get(Object key)
Returns the value set for the given key or null if there is no such mapping or if the
mapping value is null . |
|
int |
getIndexOfKey(K key)
Returns the index of the given key or -1 if there is no such key. |
|
K |
getKey(int index)
Returns the key at the given index or null if out of bounds. |
|
V |
getValue(int index)
Returns the value at the given index or null if out of bounds. |
|
static
|
of(Object... keyValuePairs)
Returns a new instance of an array map of the given key value pairs in alternating order. |
|
V |
put(K key,
V value)
Sets the value for the given key, overriding any existing value. |
|
V |
remove(int index)
Removes the key/value mapping at the given index, or ignored if the index is out of bounds. |
|
V |
remove(Object key)
Removes the key-value pair of the given key, or ignore if the key cannot be found. |
|
V |
set(int index,
K key,
V value)
Sets the key/value mapping at the given index, overriding any existing key/value mapping. |
|
V |
set(int index,
V value)
Sets the value at the given index, overriding any existing value mapping. |
|
int |
size()
Returns the number of key-value pairs set. |
|
void |
trim()
Trims the internal array storage to minimize memory usage. |
Methods inherited from class java.util.AbstractMap |
---|
equals, hashCode, isEmpty, keySet, putAll, toString, values |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public ArrayMap()
Method Detail |
---|
public static <K,V> ArrayMap<K,V> create()
ArrayMap<String, String> map = ArrayMap.create();
.
public static <K,V> ArrayMap<K,V> create(int initialCapacity)
ArrayMap<String, String> map = ArrayMap.create(8);
.
public static <K,V> ArrayMap<K,V> of(Object... keyValuePairs)
ArrayMap<String, String> map = ArrayMap.of("key1", "value1", "key2", "value2",
...);
.
WARNING: there is no compile-time checking of the keyValuePairs
parameter to ensure
that the keys or values have the correct type, so if the wrong type is passed in, any problems
will occur at runtime. Also, there is no checking that the keys are unique, which the caller
must ensure is true.
public final int size()
size
in interface Map<K,V>
size
in class AbstractMap<K,V>
public final K getKey(int index)
null
if out of bounds.
public final V getValue(int index)
null
if out of bounds.
public final V set(int index, K key, V value)
There is no checking done to ensure that the key does not already exist. Therefore, this method is dangerous to call unless the caller can be certain the key does not already exist in the map.
null
for none
IndexOutOfBoundsException
- if index is negativepublic final V set(int index, V value)
null
for none
IndexOutOfBoundsException
- if index is negative or >=
sizepublic final void add(K key, V value)
set(size(),
key, value)
.
IndexOutOfBoundsException
- if index is negativepublic final V remove(int index)
null
for nonepublic final boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
public final int getIndexOfKey(K key)
-1
if there is no such key.
public final V get(Object key)
null
if there is no such mapping or if the
mapping value is null
.
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
public final V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
null
for nonepublic final V remove(Object key)
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
null
for nonepublic final void trim()
public final void ensureCapacity(int minCapacity)
public void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
public final boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
public final Set<Map.Entry<K,V>> entrySet()
entrySet
in interface Map<K,V>
entrySet
in class AbstractMap<K,V>
public ArrayMap<K,V> clone()
clone
in class AbstractMap<K,V>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |