Class ArrayMap<K,V>
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
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.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final class(package private) final class(package private) final classNested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidAdds the key/value mapping at the end of the list.voidclear()clone()final booleancontainsKey(Object key) Returns whether there is a mapping for the given key.final booleancontainsValue(Object value) static <K,V> ArrayMap <K, V> create()Returns a new instance of an array map with initial capacity of zero.static <K,V> ArrayMap <K, V> create(int initialCapacity) Returns a new instance of an array map of the given initial capacity.final voidensureCapacity(int minCapacity) Ensures that the capacity of the internal arrays is at least a given capacity.entrySet()final VReturns the value set for the given key ornullif there is no such mapping or if the mapping value isnull.private intgetDataIndexOfKey(Object key) Returns the data index of the given key or-2if there is no such key.final intgetIndexOfKey(K key) Returns the index of the given key or-1if there is no such key.final KgetKey(int index) Returns the key at the given index ornullif out of bounds.final VgetValue(int index) Returns the value at the given index ornullif out of bounds.static <K,V> ArrayMap <K, V> Returns a new instance of an array map of the given key value pairs in alternating order.final VSets the value for the given key, overriding any existing value.final Vremove(int index) Removes the key/value mapping at the given index, or ignored if the index is out of bounds.final VRemoves the key-value pair of the given key, or ignore if the key cannot be found.private VremoveFromDataIndexOfKey(int dataIndexOfKey) Removes the key/value mapping at the given data index of key, or ignored if the index is out of bounds.final VSets the key/value mapping at the given index, overriding any existing key/value mapping.final VSets the value at the given index, overriding any existing value mapping.private voidprivate voidsetDataCapacity(int newDataCapacity) final intsize()Returns the number of key-value pairs set.final voidtrim()Trims the internal array storage to minimize memory usage.private VvalueAtDataIndex(int dataIndex) Methods inherited from class AbstractMap
equals, hashCode, isEmpty, keySet, putAll, toString, valuesMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
size
int size -
data
-
-
Constructor Details
-
ArrayMap
public ArrayMap()
-
-
Method Details
-
create
Returns a new instance of an array map with initial capacity of zero. Equivalent to calling the default constructor, except without the need to specify the type parameters. For example:ArrayMap<String, String> map = ArrayMap.create();. -
create
Returns a new instance of an array map of the given initial capacity. For example:ArrayMap<String, String> map = ArrayMap.create(8);. -
of
Returns a new instance of an array map of the given key value pairs in alternating order. For example:ArrayMap<String, String> map = ArrayMap.of("key1", "value1", "key2", "value2", ...);.WARNING: there is no compile-time checking of the
keyValuePairsparameter 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. -
size
-
getKey
Returns the key at the given index ornullif out of bounds. -
getValue
Returns the value at the given index ornullif out of bounds. -
set
Sets the key/value mapping at the given index, overriding any existing key/value mapping.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.
- Returns:
- previous value or
nullfor none - Throws:
IndexOutOfBoundsException- if index is negative
-
set
Sets the value at the given index, overriding any existing value mapping.- Returns:
- previous value or
nullfor none - Throws:
IndexOutOfBoundsException- if index is negative or>=size
-
add
Adds the key/value mapping at the end of the list. Behaves identically toset(size(), key, value).- Throws:
IndexOutOfBoundsException- if index is negative
-
remove
Removes the key/value mapping at the given index, or ignored if the index is out of bounds.- Returns:
- previous value or
nullfor none
-
containsKey
Returns whether there is a mapping for the given key.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractMap<K,V>
-
getIndexOfKey
Returns the index of the given key or-1if there is no such key. -
get
-
put
-
remove
-
trim
public final void trim()Trims the internal array storage to minimize memory usage. -
ensureCapacity
public final void ensureCapacity(int minCapacity) Ensures that the capacity of the internal arrays is at least a given capacity. -
setDataCapacity
private void setDataCapacity(int newDataCapacity) -
setData
-
valueAtDataIndex
-
getDataIndexOfKey
Returns the data index of the given key or-2if there is no such key. -
removeFromDataIndexOfKey
Removes the key/value mapping at the given data index of key, or ignored if the index is out of bounds. -
clear
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V> - Overrides:
containsValuein classAbstractMap<K,V>
-
entrySet
-
clone
-