Package host.anzo.commons.collection
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 mapV- the type of mapped values
- All Implemented Interfaces:
Serializable,Cloneable,Map<K,,V> SequencedMap<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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all entries from this map and the internal key list.getKeyAt(int index) Retrieves the key at the specified index in the insertion order list.intgetKeyIndex(K key) Returns the index of the specified key in the insertion order list.getKeys()Returns a copy of the internal key list to prevent external modification.getValueAt(int index) Retrieves the value at the specified index in the insertion order list.Associates the specified value with the specified key in this map.voidCopies all entries from the specified map to this map.Removes the mapping for the specified key and updates the internal key list.Methods inherited from class java.util.LinkedHashMap
containsValue, entrySet, forEach, get, getOrDefault, keySet, newLinkedHashMap, putFirst, putLast, removeEldestEntry, replaceAll, reversed, sequencedEntrySet, sequencedKeySet, sequencedValues, valuesMethods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, containsKey, isEmpty, merge, newHashMap, putIfAbsent, remove, replace, replace, sizeMethods inherited from class java.util.AbstractMap
equals, hashCode, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, containsKey, equals, hashCode, isEmpty, merge, putIfAbsent, remove, replace, replace, sizeMethods inherited from interface java.util.SequencedMap
firstEntry, lastEntry, pollFirstEntry, pollLastEntry
-
Constructor Details
-
IndexableHashMap
public IndexableHashMap()
-
-
Method Details
-
put
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:
putin interfaceMap<K,V> - Overrides:
putin classHashMap<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
nullif none - Throws:
NullPointerException- if the key or value is null
-
putAll
Copies all entries from the specified map to this map. Ensures keys are added to the key list if not already present. -
clear
public void clear()Clears all entries from this map and the internal key list. -
getKeys
Returns a copy of the internal key list to prevent external modification. The list reflects keys in insertion order.- Returns:
- a new
Listcontaining the keys in insertion order
-
getKeyIndex
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
-1if not found
-
getKeyAt
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
nullif the index is out of bounds
-
getValueAt
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
nullif the index is invalid or the key has no mapping
-
remove
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.
-