类 ArrayMap<K,V>
- 所有已实现的接口:
Map<K,V>
HashMap.
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).
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.
-
嵌套类概要
-
字段概要
字段 -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明voidSpecial fast path for appending items to the end of the array without validation.voidclear()Make the array map empty.booleancontainsAll(Collection<?> collection) Determine if the array map contains all of the keys in the given collection.booleancontainsKey(Object key) Check whether a key exists in the array.booleancontainsValue(Object value) Check whether a value exists in the array.voidensureCapacity(int minimumCapacity) Ensure the array map can hold at least minimumCapacity items.entrySet()Return aSetfor iterating over and interacting with all mappings in the array map.booleanvoiderase()Retrieve a value from the array.inthashCode()intindexOfKey(Object key) Returns the index of a key in the set.intindexOfValue(Object value) Returns an index for whichvalueAt(int)would return the specified value, or a negative number if no keys map to the specified value.booleanisEmpty()Return true if the array map contains no items.keyAt(int index) Return the key at the given index in the array.keySet()Return aSetfor iterating over and interacting with all keys in the array map.Add a new value to the array map.voidPerform aput(Object, Object)of all key/value pairs in mapvoidPerform aput(Object, Object)of all key/value pairs in arrayRemove an existing key from the array map.booleanremoveAll(Collection<?> collection) Remove all keys in the array map that exist in the given collection.removeAt(int index) Remove the key/value mapping at the given index.booleanretainAll(Collection<?> collection) Remove all keys in the array map that do not exist in the given collection.setValueAt(int index, V value) Set the value at a given index in the array.intsize()Return the number of items in this array map.toString()voidvalidate()The use of theappend(K, V)function can result in invalid array maps, in particular an array map where the same key appears multiple times.valueAt(int index) Return the value at the given index in the array.values()Return aCollectionfor iterating over and interacting with all values in the array map.从接口继承的方法 java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
字段详细资料
-
EMPTY
-
-
构造器详细资料
-
ArrayMap
public ArrayMap()Create a new empty ArrayMap. The default capacity of an array map is 0, and will grow once items are added to it. -
ArrayMap
public ArrayMap(int capacity) Create a new ArrayMap with a given initial capacity. -
ArrayMap
public ArrayMap(int capacity, boolean identityHashCode) -
ArrayMap
Create a new ArrayMap with the mappings from the given ArrayMap.
-
-
方法详细资料
-
clear
public void clear()Make the array map empty. All storage is released. -
erase
public void erase() -
ensureCapacity
public void ensureCapacity(int minimumCapacity) Ensure the array map can hold at least minimumCapacity items. -
containsKey
Check whether a key exists in the array.- 指定者:
containsKey在接口中Map<K,V> - 参数:
key- The key to search for.- 返回:
- Returns true if the key exists, else false.
-
indexOfKey
Returns the index of a key in the set.- 参数:
key- The key to search for.- 返回:
- Returns the index of the key if it exists, else a negative integer.
-
indexOfValue
Returns an index for whichvalueAt(int)would return the specified value, or a negative number if no keys map to the specified value. Beware that this is a linear search, unlike lookups by key, and that multiple keys can map to the same value and this will find only one of them. -
containsValue
Check whether a value exists in the array. This requires a linear search through the entire array.- 指定者:
containsValue在接口中Map<K,V> - 参数:
value- The value to search for.- 返回:
- Returns true if the value exists, else false.
-
get
Retrieve a value from the array. -
keyAt
Return the key at the given index in the array.- 参数:
index- The desired index, must be between 0 andsize()-1.- 返回:
- Returns the key stored at the given index.
-
valueAt
Return the value at the given index in the array.- 参数:
index- The desired index, must be between 0 andsize()-1.- 返回:
- Returns the value stored at the given index.
-
setValueAt
Set the value at a given index in the array.- 参数:
index- The desired index, must be between 0 andsize()-1.value- The new value to store at this index.- 返回:
- Returns the previous value at the given index.
-
isEmpty
public boolean isEmpty()Return true if the array map contains no items. -
put
Add a new value to the array map. -
append
Special fast path for appending items to the end of the array without validation. The array must already be large enough to contain the item. -
validate
public void validate()The use of theappend(K, V)function can result in invalid array maps, in particular an array map where the same key appears multiple times. This function verifies that the array map is valid, throwing IllegalArgumentException if a problem is found. The main use for this method is validating an array map after unpacking from an IPC, to protect against malicious callers. -
putAll
Perform aput(Object, Object)of all key/value pairs in array- 参数:
array- The array whose contents are to be retrieved.
-
remove
Remove an existing key from the array map. -
removeAt
Remove the key/value mapping at the given index.- 参数:
index- The desired index, must be between 0 andsize()-1.- 返回:
- Returns the value that was stored at this index.
-
size
public int size()Return the number of items in this array map. -
equals
This implementation returns false if the object is not a map, 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.
-
hashCode
public int hashCode() -
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.
-
containsAll
Determine if the array map contains all of the keys in the given collection.- 参数:
collection- The collection whose contents are to be checked against.- 返回:
- Returns true if this array map contains a key for every entry in collection, else returns false.
-
putAll
Perform aput(Object, Object)of all key/value pairs in map -
removeAll
Remove all keys in the array map that exist in the given collection.- 参数:
collection- The collection whose contents are to be used to remove keys.- 返回:
- Returns true if any keys were removed from the array map, else false.
-
retainAll
Remove all keys in the array map that do not exist in the given collection.- 参数:
collection- The collection whose contents are to be used to determine which keys to keep.- 返回:
- Returns true if any keys were removed from the array map, else false.
-
entrySet
Return aSetfor 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 and allocates additional state information associated with the container that will remain for the life of the container.
Note:
the semantics of this Set are subtly different than that of aHashMap: most important, theMap.Entryobject returned by its iterator is a single object that exists for the entire iterator, so you can not hold on to it after callingIterator.next. -
keySet
Return aSetfor 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 and allocates additional state information associated with the container that will remain for the life of the container.
-
values
Return aCollectionfor 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 and allocates additional state information associated with the container that will remain for the life of the container.
-