Class IndexableHashMap<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
host.anzo.commons.collection.IndexableHashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, SequencedMap<K,V>

public class IndexableHashMap<K,V> extends LinkedHashMap<K,V>
A LinkedHashMap implementation with index-based access to keys and values. Maintains insertion order like LinkedHashMap while allowing retrieval of elements by index via getKeyAt(index) and getValueAt(index).
Since:
5/16/2024
See Also:
  • Constructor Details

    • IndexableHashMap

      public IndexableHashMap()
  • Method Details

    • put

      public V put(@NotNull K key, @NotNull V value)
      Associates the specified value with the specified key in this map. If the key is not already present in the map, it is added to the key list.
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class HashMap<K,V>
      Parameters:
      key - the key with which the specified value is to be associated (non-null)
      value - the value to be associated with the specified key (non-null)
      Returns:
      the previous value associated with the key, or null if none
      Throws:
      NullPointerException - if the key or value is null
    • putAll

      public void putAll(Map<? extends K,? extends V> m)
      Copies all entries from the specified map to this map. Ensures keys are added to the key list if not already present.
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class HashMap<K,V>
      Parameters:
      m - the map whose entries are to be added to this map
    • clear

      public void clear()
      Clears all entries from this map and the internal key list.
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class LinkedHashMap<K,V>
    • getKeys

      public List<K> getKeys()
      Returns a copy of the internal key list to prevent external modification. The list reflects keys in insertion order.
      Returns:
      a new List containing the keys in insertion order
    • getKeyIndex

      public int getKeyIndex(K key)
      Returns the index of the specified key in the insertion order list.
      Parameters:
      key - the key to search for
      Returns:
      the index of the key, or -1 if not found
    • getKeyAt

      public K getKeyAt(int index)
      Retrieves the key at the specified index in the insertion order list.
      Parameters:
      index - the index of the key to retrieve
      Returns:
      the key at the specified index, or null if the index is out of bounds
    • getValueAt

      public V getValueAt(int index)
      Retrieves the value at the specified index in the insertion order list.
      Parameters:
      index - the index of the value to retrieve
      Returns:
      the value associated with the key at the specified index, or null if the index is invalid or the key has no mapping
    • remove

      public V remove(@NotNull @NotNull Object key)
      Removes the mapping for the specified key and updates the internal key list.

      Performance Note: This method rebuilds the key list, which may be inefficient for frequent remove operations.

      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class HashMap<K,V>
      Parameters:
      key - the key to remove (non-null)
      Returns:
      the previous value associated with the key, or null if none