public final class ArrayMap<K,V> extends Object implements 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.
Constructor and Description |
---|
ArrayMap()
Create a new empty ArrayMap.
|
ArrayMap(ArrayMap<K,V> map)
Create a new ArrayMap with the mappings from the given ArrayMap.
|
ArrayMap(int capacity)
Create a new ArrayMap with a given initial capacity.
|
ArrayMap(int capacity,
boolean identityHashCode) |
Modifier and Type | Method and Description |
---|---|
void |
append(K key,
V value)
Special fast path for appending items to the end of the array without validation.
|
void |
clear()
Make the array map empty.
|
boolean |
containsAll(Collection<?> collection)
Determine if the array map contains all of the keys in the given collection.
|
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.
|
Set<Map.Entry<K,V>> |
entrySet()
Return a
Set for iterating over and interacting with all mappings
in the array map. |
boolean |
equals(Object object)
Indicates whether some other object is "equal to" this one.
|
void |
erase() |
V |
get(Object key)
Retrieve a value from the array.
|
int |
hashCode()
Returns a hash code value for the object.
|
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.
|
Set<K> |
keySet()
Return a
Set for iterating over and interacting with all keys
in the array map. |
V |
put(K key,
V value)
Add a new value to the array map.
|
void |
putAll(ArrayMap<? extends K,? extends V> array)
Perform a
put(Object, Object) of all key/value pairs in array |
void |
putAll(Map<? extends K,? extends V> map)
Perform a
put(Object, Object) of all key/value pairs in map |
V |
remove(Object key)
Remove an existing key from the array map.
|
boolean |
removeAll(Collection<?> collection)
Remove all keys in the array map that exist in the given collection.
|
V |
removeAt(int index)
Remove the key/value mapping at the given index.
|
boolean |
retainAll(Collection<?> collection)
Remove all keys in the array map that do not exist in the given collection.
|
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 representation of the object.
|
void |
validate()
The use of the
append(K, V) function can result in invalid array maps, in particular
an array map where the same key appears multiple times. |
V |
valueAt(int index)
Return the value at the given index in the array.
|
Collection<V> |
values()
Return a
Collection for iterating over and interacting with all values
in the array map. |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public static final ArrayMap EMPTY
public ArrayMap()
public ArrayMap(int capacity)
public ArrayMap(int capacity, boolean identityHashCode)
public void clear()
public void erase()
public void ensureCapacity(int minimumCapacity)
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
key
- The key to search for.public int indexOfKey(Object key)
key
- The key to search for.public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
value
- The value to search for.public K keyAt(int index)
index
- The desired index, must be between 0 and size()
-1.public V valueAt(int index)
index
- The desired index, must be between 0 and size()
-1.public V setValueAt(int index, V value)
index
- The desired index, must be between 0 and size()
-1.value
- The new value to store at this index.public boolean isEmpty()
public V put(K key, V value)
put
in interface Map<K,V>
key
- The key under which to store the value. If
this key already exists in the array, its value will be replaced.value
- The value to store for the given key.public void append(K key, V value)
public void validate()
append(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.public void putAll(ArrayMap<? extends K,? extends V> array)
put(Object, Object)
of all key/value pairs in arrayarray
- The array whose contents are to be retrieved.public V removeAt(int index)
index
- The desired index, must be between 0 and size()
-1.public int size()
public boolean equals(Object object)
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
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.
public int hashCode()
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in interface Map<K,V>
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
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.
public boolean containsAll(Collection<?> collection)
collection
- The collection whose contents are to be checked against.public void putAll(Map<? extends K,? extends V> map)
put(Object, Object)
of all key/value pairs in mappublic boolean removeAll(Collection<?> collection)
collection
- The collection whose contents are to be used to remove keys.public boolean retainAll(Collection<?> collection)
collection
- The collection whose contents are to be used to determine which
keys to keep.public Set<Map.Entry<K,V>> entrySet()
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 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,
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()
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 and allocates additional state information associated with the container that will remain for the life of the container.
public Collection<V> values()
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 and allocates additional state information associated with the container that will remain for the life of the container.