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 OrderedMap#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 |
|---|---|
| public static class | |
| public static class | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Array |
| Access | Constructor and Description |
|---|---|
| public | |
| public | OrderedMap(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity)Creates a new map with a load factor of 0.8. |
| public | OrderedMap(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity, float loadFactor)Creates a new map with the specified initial capacity and load factor. |
| public | OrderedMap(OrderedMap<? extends K, ? extends V> map)
Creates a new map containing the items in the specified map. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true ifbefore was removed and after was added, false otherwisea key that must be present for this to succeed before, K a key that must not be in this map for this to succeed after)Changes the key |
| public boolean | Returns: true ifafter successfully replaced the key at index, false otherwisethe index in the order of the key to change; must be non-negative and less than index, K sizethe key that will replace the contents at after)index; this key must not be present for this to succeedChanges the key at the given |
| public void | clear(int maximumCapacity)
Overrides com. Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. |
| public void | |
| public ObjectMap. | entries()
Overrides com. Returns an iterator for the entries in the map. |
| public ObjectMap. | iterator()
Overrides com. Implements java. Returns an iterator over elements of type |
| public ObjectMap. | |
| public Array | |
| public V | put(K key, V value)
Overrides com. Returns the old value associated with the specified key, or null. |
| public <T extends K> void | |
| public V | |
| public V | remove(K key)
Overrides com. Returns the value for the removed key, or null if the key is not in the map. |
| public V | |
| protected String | |
| public ObjectMap. | values()
Overrides com. Returns an iterator for the values in the map. |
| keys | back to summary |
|---|---|
| pack-priv final Array<K> keys | |
| OrderedMap | back to summary |
|---|---|
| public OrderedMap() Creates a new map with an initial capacity of 51 and a load factor of 0.8. | |
| OrderedMap | back to summary |
|---|---|
| public OrderedMap(int initialCapacity) Creates a new map with a load factor of 0.8.
| |
| OrderedMap | back to summary |
|---|---|
| public OrderedMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor. This map will hold initialCapacity items before growing the backing table.
| |
| OrderedMap | back to summary |
|---|---|
| public OrderedMap(OrderedMap<? extends K, ? extends V> map) Creates a new map containing the items in the specified map. | |
| alter | back to summary |
|---|---|
| public boolean alter(K before, K after) Changes the key
| |
| alterIndex | back to summary |
|---|---|
| public boolean alterIndex(int index, K after) Changes the key at the given
| |
| clear | back to summary |
|---|---|
| public void clear(int maximumCapacity) Overrides com. Doc from com. Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. | |
| clear | back to summary |
|---|---|
| public void clear() Overrides com. | |
| entries | back to summary |
|---|---|
| public ObjectMap. Overrides com. Returns an iterator for the entries in the map. Remove is supported.
If | |
| iterator | back to summary |
|---|---|
| public ObjectMap. Overrides com. Implements java. Doc from java. Returns an iterator over elements of type | |
| keys | back to summary |
|---|---|
| public ObjectMap. Overrides com. Returns an iterator for the keys in the map. Remove is supported.
If | |
| orderedKeys | back to summary |
|---|---|
| public Array | |
| put | back to summary |
|---|---|
| public V put(K key, V value) Overrides com. Doc from com. Returns the old value associated with the specified key, or null. | |
| putAll | back to summary |
|---|---|
| public <T extends K> void putAll(OrderedMap<T, ? extends V> map) | |
| putMissing | back to summary |
|---|---|
| public V putMissing(K key, V value) Overrides com.
| |
| remove | back to summary |
|---|---|
| public V remove(K key) Overrides com. Doc from com. Returns the value for the removed key, or null if the key is not in the map. | |
| removeIndex | back to summary |
|---|---|
| public V removeIndex(int index) | |
| toString | back to summary |
|---|---|
| protected String toString(String separator, boolean braces) Overrides com. | |
| values | back to summary |
|---|---|
| public ObjectMap. Overrides com. Returns an iterator for the values in the map. Remove is supported.
If | |
| Modifier and Type | Field and Description |
|---|---|
| private Array |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public ObjectMap. | next()
Overrides com. Implements java. Note the same entry instance is returned each time this method is called. |
| public void | remove()
Overrides com. Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| public void |
| keys | back to summary |
|---|---|
| private Array<K> keys | |
| OrderedMapEntries | back to summary |
|---|---|
| public OrderedMapEntries(OrderedMap<K, V> map) | |
| next | back to summary |
|---|---|
| public ObjectMap. Overrides com. Implements java. Doc from com. Note the same entry instance is returned each time this method is called. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides com. Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the | |
| reset | back to summary |
|---|---|
| public void reset() Overrides com. | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public K | next()
Overrides com. Implements java. Returns the next element in the iteration. |
| public void | remove()
Overrides com. Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| public void | |
| public Array | toArray(Array<K> array)
Overrides com. Adds the remaining keys to the array. |
| public Array | toArray()
Overrides com. Returns a new array containing the remaining keys. |
| keys | back to summary |
|---|---|
| private Array<K> keys | |
| OrderedMapKeys | back to summary |
|---|---|
| public OrderedMapKeys(OrderedMap<K, ?> map) | |
| next | back to summary |
|---|---|
| public K next() Overrides com. Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides com. Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the | |
| reset | back to summary |
|---|---|
| public void reset() Overrides com. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Adds the remaining keys to the array. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Returns a new array containing the remaining keys. | |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public V | next()
Overrides com. Implements java. Returns the next element in the iteration. |
| public void | remove()
Overrides com. Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| public void | |
| public Array | toArray(Array<V> array)
Overrides com. Adds the remaining values to the specified array. |
| public Array | toArray()
Overrides com. Returns a new array containing the remaining values. |
| keys | back to summary |
|---|---|
| private Array<T> keys | |
| OrderedMapValues | back to summary |
|---|---|
| public OrderedMapValues(OrderedMap<?, V> map) | |
| next | back to summary |
|---|---|
| public V next() Overrides com. Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides com. Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the | |
| reset | back to summary |
|---|---|
| public void reset() Overrides com. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Adds the remaining values to the specified array. | |
| toArray | back to summary |
|---|---|
| public Array Overrides com. Doc from com. Returns a new array containing the remaining values. | |