Class InsertionOrderMap<K,​V>

  • Type Parameters:
    K - the generic type of keys maintained by this map
    V - the generic type of mapped values
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.util.Map<K,​V>

    public class InsertionOrderMap<K,​V>
    extends java.util.LinkedHashMap<K,​V>
    The class InsertionOrderMap overwrites the put-method from the LinkedHashMap. That inserts the value to the right order it was inserted in the Map. Note that the difference in the LinkedHashMap is the order does not change if we put the same key with a new value. In this class the order changes when we put a new value with the same key.
    See Also:
    LinkedHashMap, Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
    • Constructor Summary

      Constructors 
      Constructor Description
      InsertionOrderMap()
      Constructs an empty insertion-ordered InsertionOrderMap instance with a default capacity (16) and load factor (0.75).
      InsertionOrderMap​(int initialCapacity)
      Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and a default load factor (0.75).
      InsertionOrderMap​(int initialCapacity, float loadFactor)
      Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and load factor.
      InsertionOrderMap​(int initialCapacity, float loadFactor, boolean accessOrder)
      Constructs an empty InsertionOrderMap instance with the specified initial capacity, load factor and ordering mode.
      InsertionOrderMap​(java.util.Map<? extends K,​? extends V> m)
      Constructs an insertion-ordered InsertionOrderMap instance with the same mappings as the specified map.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      V put​(K key, V value)
      Associates the specified value with the specified key in this map.
      • Methods inherited from class java.util.LinkedHashMap

        clear, containsValue, entrySet, forEach, get, getOrDefault, keySet, removeEldestEntry, replaceAll, values
      • Methods inherited from class java.util.HashMap

        clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
      • Methods inherited from class java.util.AbstractMap

        equals, hashCode, toString
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, putAll, putIfAbsent, remove, remove, replace, replace, size
    • Constructor Detail

      • InsertionOrderMap

        public InsertionOrderMap()
        Constructs an empty insertion-ordered InsertionOrderMap instance with a default capacity (16) and load factor (0.75).
      • InsertionOrderMap

        public InsertionOrderMap​(int initialCapacity)
        Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and a default load factor (0.75).
        Parameters:
        initialCapacity - the initial capacity.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative.
      • InsertionOrderMap

        public InsertionOrderMap​(int initialCapacity,
                                 float loadFactor)
        Constructs an empty insertion-ordered InsertionOrderMap instance with the specified initial capacity and load factor.
        Parameters:
        initialCapacity - the initial capacity.
        loadFactor - the load factor.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.
      • InsertionOrderMap

        public InsertionOrderMap​(int initialCapacity,
                                 float loadFactor,
                                 boolean accessOrder)
        Constructs an empty InsertionOrderMap instance with the specified initial capacity, load factor and ordering mode.
        Parameters:
        initialCapacity - the initial capacity.
        loadFactor - the load factor.
        accessOrder - the ordering mode - true for access-order, false for insertion-order.
        Throws:
        java.lang.IllegalArgumentException - if the initial capacity is negative or the load factor is nonpositive.
      • InsertionOrderMap

        public InsertionOrderMap​(java.util.Map<? extends K,​? extends V> m)
        Constructs an insertion-ordered InsertionOrderMap instance with the same mappings as the specified map. The InsertionOrderMap instance is created with a a default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.
        Parameters:
        m - the map whose mappings are to be placed in this map.
        Throws:
        java.lang.NullPointerException - if the specified map is null.
    • Method Detail

      • put

        public V put​(K key,
                     V value)
        Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is removed and then insert new with the same key and the new value. That inserts the value to the right order it was inserted in the Map. Note that the difference in the LinkedHashMap is the order does not change if we put the same key with a new value. In this class the order changes when we put a new value with the same key.
        Specified by:
        put in interface java.util.Map<K,​V>
        Overrides:
        put in class java.util.HashMap<K,​V>
        Parameters:
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
        Returns:
        previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.