K - the type of keys maintained by this mapV - the type of mapped valuespublic class ArrayMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.io.Serializable
Hash table and array implementation of the Map interface,
with predictable iteration order. This implementation is similar to
LinkedHashMap, but uses a more compact memory representation
originally pioneered by PyPy, and subsequently
adopted in Python 3.6.
This class provides all of the optional Map operations, and
permits null elements. Like HashMap, it provides constant-time
performance for the basic operations (add, contains and
remove), assuming the hash function disperses elements
properly among the buckets. Performance is typically within 5% of
HashMap, with only around a half of the memory overhead
(a third of LinkedHashMap).
Unlike HashMap and LinkedHashMap, this class does not cache the hash code value of its keys, as this is typically redundant: value types are a trivial transformation, while Strings already cache their hash values. This may however result in a significant negative performance impact if key hashCode/equality checks are expensive.
Note that this implementation is not synchronized.
If multiple threads access an AraryMap concurrently, and at least
one of the threads modifies the map structurally, it must be
synchronized externally. This is typically accomplished by
synchronizing on some object that naturally encapsulates the map.
If no such object exists, the map should be "wrapped" using the
Collections.synchronizedMap
method. This is best done at creation time, to prevent accidental
unsynchronized access to the map:
Map<...> m = Collections.synchronizedMap(new ArrayMap<>(...));A structural modification is any operation that adds or deletes one or more mappings or, in the case of access-ordered linked hash maps, affects iteration order. Merely changing the value associated with a key that is already contained in the map is not a structural modification.
The iterators returned by the iterator method of the collections
returned by all of this class's collection view methods are
fail-fast: if the map is structurally modified at any time after
the iterator is created, in any way except through the iterator's own
remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent
modification, the iterator fails quickly and cleanly, rather than risking
arbitrary, non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
Map,
HashMap,
TreeMap,
Serialized Form| Constructor and Description |
|---|
ArrayMap()
Constructs an empty map with an initial capacity of ten.
|
ArrayMap(java.util.Map<? extends K,? extends V> entries)
Constructs a map with the same mappings as the specified map.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsKey(java.lang.Object key) |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet() |
void |
forEach(java.util.function.BiConsumer<? super K,? super V> action) |
V |
get(java.lang.Object key) |
V |
put(K key,
V value) |
V |
remove(java.lang.Object o) |
int |
size() |
static <K,V> ArrayMap<K,V> |
withInitialCapacity(int initialCapacity)
Constructs an empty map with the specified initial capacity.
|
clear, clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, valuespublic ArrayMap()
public static <K,V> ArrayMap<K,V> withInitialCapacity(int initialCapacity)
K - the type of keys maintained by the mapV - the type of mapped valuesinitialCapacity - the initial capacity of the mapjava.lang.IllegalArgumentException - if the specified initial capacity
is negativepublic int size()
public boolean containsKey(java.lang.Object key)
public V get(java.lang.Object key)
public V remove(java.lang.Object o)