Class OrderedMap<K,​V>

  • All Implemented Interfaces:
    java.lang.Iterable<ObjectMap.Entry<K,​V>>

    public class OrderedMap<K,​V>
    extends ObjectMap<K,​V>
    An 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.

    • Constructor Detail

      • OrderedMap

        public OrderedMap()
        Creates a new map with an initial capacity of 51 and a load factor of 0.8.
      • OrderedMap

        public OrderedMap​(int initialCapacity)
        Creates a new map with a load factor of 0.8.
        Parameters:
        initialCapacity - The backing array size is initialCapacity / loadFactor, increased to the next power of two.
      • OrderedMap

        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.
        Parameters:
        initialCapacity - The backing array size is initialCapacity / loadFactor, increased to the next power of two.
      • OrderedMap

        public OrderedMap​(OrderedMap<? extends K,​? extends V> map)
        Creates a new map containing the items in the specified map.
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Description copied from class: ObjectMap
        Returns the old value associated with the specified key, or null.
        Overrides:
        put in class ObjectMap<K,​V>
      • putAll

        public <T extends K> void putAll​(OrderedMap<T,​? extends V> map)
      • remove

        public V remove​(K key)
        Description copied from class: ObjectMap
        Returns the value for the removed key, or null if the key is not in the map.
        Overrides:
        remove in class ObjectMap<K,​V>
      • removeIndex

        public V removeIndex​(int index)
      • alter

        public boolean alter​(K before,
                             K after)
        Changes the key 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.
        Parameters:
        before - a key that must be present for this to succeed
        after - a key that must not be in this map for this to succeed
        Returns:
        true if before was removed and after was added, false otherwise
      • alterIndex

        public 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. 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.
        Parameters:
        index - the index in the order of the key to change; must be non-negative and less than ObjectMap.size
        after - the key that will replace the contents at index; this key must not be present for this to succeed
        Returns:
        true if after successfully replaced the key at index, false otherwise
      • clear

        public void clear​(int maximumCapacity)
        Description copied from class: ObjectMap
        Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
        Overrides:
        clear in class ObjectMap<K,​V>
      • orderedKeys

        public Array<K> orderedKeys()
      • toString

        protected java.lang.String toString​(java.lang.String separator,
                                            boolean braces)
        Overrides:
        toString in class ObjectMap<K,​V>