K
- the type of the keyV
- the type of the valuepublic class OrderedFastHashMap<K,V> extends Object implements Map<K,V>, Serializable
This map does not support null and it is not thread-safe. It implements the map interface but only for compatibility reason in the sense of replacing a regular map. Iterator and streaming methods are either not implemented or less efficient.
It goes the extra mile to avoid the overhead of wrapper objects.
Because you typically know what you do, we run minimal index checks only and rely on the default exceptions by Java. Why should we do things twice?
Important Note: This is meant for small maps because to save on memory allocation and churn, we are not keeping a wrapper for a reference from the map to the list, only from the list to the map. Hence when you remove a key, we have to iterate the entire list. Mostly, half of it most likely, but still expensive enough. When you have something small like 10 to 20 entries, this won't matter that much especially when a remove might be a rare event.
This is based on FashHashMap from XLT which is based on a version from: https://github.com/mikvor/hashmapTest/blob/master/src/main/java/map/objobj/ObjObjMap.java No concrete license specified at the source. The project is public domain.
Constructor and Description |
---|
OrderedFastHashMap()
Default constructor which create an ordered map with default size.
|
OrderedFastHashMap(int size)
Custom constructor to get a map with a custom size and fill factor.
|
Modifier and Type | Method and Description |
---|---|
V |
add(K key,
V value)
Append at the end.
|
V |
addFirst(K key,
V value)
Insert at the beginning.
|
V |
addLast(K key,
V value)
Append at the end.
|
void |
clear()
Clears the map, reuses the data structure by clearing it out.
|
boolean |
containsKey(Object key)
Checks of a key is in the map.
|
boolean |
containsValue(Object value) |
Set<Map.Entry<K,V>> |
entrySet() |
V |
get(Object key)
Get a value for a key, any key type is permitted due to
the nature of the Map interface.
|
org.htmlunit.util.OrderedFastHashMap.Entry<K,V> |
getEntry(int index)
Returns an entry consisting of key and value at a given position.
|
V |
getFirst() |
K |
getKey(int index)
Returns the key at a certain position of the ordered list that
keeps the addition order of this map.
|
V |
getLast() |
V |
getValue(int index)
Returns the value at a certain position of the ordered list that
keeps the addition order of this map.
|
boolean |
isEmpty() |
List<K> |
keys()
Returns a list of all keys in order of addition.
|
Set<K> |
keySet() |
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> src) |
V |
remove(int index)
Removes a key and value from this map based on the position
in the backing list, rather by key as usual.
|
V |
remove(Object key)
Remove a key from the map.
|
V |
removeFirst()
Removes the first entry.
|
V |
removeLast()
Removes the last entry.
|
void |
reverse()
Just reverses the ordering of the map as created so far.
|
int |
size()
Returns the size of the map, effectively the number of entries.
|
String |
toString() |
List<V> |
values()
Returns a list of all values ordered by when the key was
added.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
public OrderedFastHashMap()
public OrderedFastHashMap(int size)
size
- the size to use, must 0 or positive, negative values default to 0public V get(Object key)
public V remove(Object key)
public int size()
public List<K> keys()
public List<V> values()
public void clear()
public org.htmlunit.util.OrderedFastHashMap.Entry<K,V> getEntry(int index)
index
- the position to fetchIndexOutOfBoundsException
- when the ask for the position is invalidpublic K getKey(int index)
index
- the position to fetchIndexOutOfBoundsException
- when the ask for the position is invalidpublic V getValue(int index)
index
- the position to fetchIndexOutOfBoundsException
- when the ask for the position is invalidpublic V remove(int index)
index
- the position to remove the data fromIndexOutOfBoundsException
- when the ask for the position is invalidpublic V addFirst(K key, V value)
key
- the keyvalue
- the valuepublic V add(K key, V value)
key
- the keyvalue
- the valuepublic V addLast(K key, V value)
key
- the keyvalue
- the valuepublic V getFirst()
public V getLast()
public V removeFirst()
public V removeLast()
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
key
- the key to checkpublic boolean containsValue(Object value)
containsValue
in interface Map<K,V>
public void reverse()
Copyright © 2002–2025 Gargoyle Software Inc.. All rights reserved.