public class OrderedMap<K,V> extends ObjectMap<K,V>
ObjectMap that also stores keys in an Array using the insertion order. Null keys are not allowed. No
allocation is done except when growing the table size.
Iteration over the entries(), keys(), and values() is ordered and faster than an unordered map. Keys
can also be accessed and the order changed using orderedKeys(). There is some additional overhead for put and remove.
When used for faster iteration versus ObjectMap and the order does not actually matter, copying during remove can be greatly
reduced by setting Array.ordered to false for orderedKeys().
This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due
to orderedKeys(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce
collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT
size.
Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
| Modifier and Type | Class and Description |
|---|---|
static class |
OrderedMap.OrderedMapEntries<K,V> |
static class |
OrderedMap.OrderedMapKeys<K> |
static class |
OrderedMap.OrderedMapValues<V> |
ObjectMap.Entries<K,V>, ObjectMap.Entry<K,V>, ObjectMap.Keys<K>, ObjectMap.Values<V>| Constructor and Description |
|---|
OrderedMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
|
OrderedMap(int initialCapacity)
Creates a new map with a load factor of 0.8.
|
OrderedMap(int initialCapacity,
float loadFactor)
Creates a new map with the specified initial capacity and load factor.
|
OrderedMap(OrderedMap<? extends K,? extends V> map)
Creates a new map containing the items in the specified map.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
alter(K before,
K after)
Changes the key
before to after without changing its position in the order or its value. |
boolean |
alterIndex(int index,
K after)
Changes the key at the given
index in the order to after, without changing the ordering of other entries or
any values. |
void |
clear() |
void |
clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
|
ObjectMap.Entries<K,V> |
entries()
Returns an iterator for the entries in the map.
|
ObjectMap.Entries<K,V> |
iterator() |
ObjectMap.Keys<K> |
keys()
Returns an iterator for the keys in the map.
|
Array<K> |
orderedKeys() |
V |
put(K key,
V value)
Returns the old value associated with the specified key, or null.
|
<T extends K> |
putAll(OrderedMap<T,? extends V> map) |
V |
remove(K key)
Returns the value for the removed key, or null if the key is not in the map.
|
V |
removeIndex(int index) |
ObjectMap.Values<V> |
values()
Returns an iterator for the values in the map.
|
containsKey, containsValue, ensureCapacity, equals, equalsIdentity, findKey, get, get, hashCode, isEmpty, notEmpty, putAll, shrink, toString, toStringforEach, spliteratorpublic OrderedMap()
public OrderedMap(int initialCapacity)
initialCapacity - If not a power of two, it is increased to the next nearest power of two.public OrderedMap(int initialCapacity,
float loadFactor)
initialCapacity - If not a power of two, it is increased to the next nearest power of two.public OrderedMap(OrderedMap<? extends K,? extends V> map)
public V put(K key, V value)
ObjectMappublic <T extends K> void putAll(OrderedMap<T,? extends V> map)
public V remove(K key)
ObjectMappublic V removeIndex(int index)
public boolean alter(K before, K after)
before to after without changing its position in the order or its value. Returns true if
after has been added to the OrderedMap and before has been removed; returns false if after is
already present or before is not present. If you are iterating over an OrderedMap and have an index, you should
prefer alterIndex(int, Object), which doesn't need to search for an index like this does and so can be faster.before - a key that must be present for this to succeedafter - a key that must not be in this map for this to succeedbefore was removed and after was added, false otherwisepublic boolean alterIndex(int index,
K after)
index in the order to after, without changing the ordering of other entries or
any values. If after is already present, this returns false; it will also return false if index is invalid
for the size of this map. Otherwise, it returns true. Unlike alter(Object, Object), this operates in constant time.index - the index in the order of the key to change; must be non-negative and less than ObjectMap.sizeafter - the key that will replace the contents at index; this key must not be present for this to succeedafter successfully replaced the key at index, false otherwisepublic void clear(int maximumCapacity)
ObjectMappublic ObjectMap.Entries<K,V> iterator()
public ObjectMap.Entries<K,V> entries()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the OrderedMap.OrderedMapEntries constructor for nested or multithreaded iteration.
public ObjectMap.Values<V> values()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the OrderedMap.OrderedMapValues constructor for nested or multithreaded iteration.
public ObjectMap.Keys<K> keys()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the OrderedMap.OrderedMapKeys constructor for nested or multithreaded iteration.
Copyright © 2021. All rights reserved.