Package org.htmlunit.util

# Class OrderedFastHashMap<K,V>

java.lang.Object
org.htmlunit.util.OrderedFastHashMap<K,V>

Type Parameters:
  `K` - the type of the key
  `V` - the type of the value

All Implemented Interfaces:
  `Serializable`, `Map<K,V>`

public class OrderedFastHashMap<K,V> extends Object implements Map<K,V>, Serializable
Simple and efficient linked map or better ordered map implementation to replace the default linked list which is heavy. 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.

See Also:
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  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.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `V`
  `add(K key, V value)`
  `V`
  `addFirst(K key, V value)`
  `V`
  `addLast(K key, V value)`
  `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()`
  `V`
  `removeLast()`
  `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.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface java.util.Map

  `compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll`

- ## Constructor Details

  - ### OrderedFastHashMap

    public OrderedFastHashMap()
    Default constructor which create an ordered map with default size.

  - ### OrderedFastHashMap

    public OrderedFastHashMap(int size)
    Custom constructor to get a map with a custom size and fill factor. We are not spending time on range checks, rather use a default if things are wrong.
    Parameters:
      `size` - the size to use, must 0 or positive, negative values default to 0

- ## Method Details

  - ### get

    public V get(Object key)
    Get a value for a key, any key type is permitted due to the nature of the Map interface.
    Specified by:
      `get` in interface `Map<K,V>`
    Parameters:
      `key` - the key
    Returns:
      the value or null, if the key does not exist

  - ### remove

    public V remove(Object key)
    Remove a key from the map. Returns the stored value or null of the key is not known.
    Specified by:
      `remove` in interface `Map<K,V>`
    Parameters:
      `key` - the key to remove
    Returns:
      the stored value or null if the key does not exist

  - ### size

    public int size()
    Returns the size of the map, effectively the number of entries.
    Specified by:
      `size` in interface `Map<K,V>`
    Returns:
      the size of the map

  - ### keys

    public List<K> keys()
    Returns a list of all keys in order of addition. This is an expensive operation, because we get a static list back that is not backed by the implementation. Changes to the returned list are not reflected in the map.
    Returns:
      a list of keys as inserted into the map

  - ### values

    public List<V> values()
    Returns a list of all values ordered by when the key was added. This is an expensive operation, because we get a static list back that is not backed by the implementation. Changes to the returned list are not reflected in the map.
    Specified by:
      `values` in interface `Map<K,V>`
    Returns:
      a list of values

  - ### clear

    public void clear()
    Clears the map, reuses the data structure by clearing it out. It won't shrink the underlying arrays!
    Specified by:
      `clear` in interface `Map<K,V>`

  - ### getEntry

    public org.htmlunit.util.OrderedFastHashMap.Entry<K,V> getEntry(int index)
    Returns an entry consisting of key and value at a given position. This position relates to the ordered key list that maintain the addition order for this map.
    Parameters:
      `index` - the position to fetch
    Returns:
      an entry of key and value
    Throws:
      `IndexOutOfBoundsException` - when the ask for the position is invalid

  - ### getKey

    public K getKey(int index)
    Returns the key at a certain position of the ordered list that keeps the addition order of this map.
    Parameters:
      `index` - the position to fetch
    Returns:
      the key at this position
    Throws:
      `IndexOutOfBoundsException` - when the ask for the position is invalid

  - ### getValue

    public V getValue(int index)
    Returns the value at a certain position of the ordered list that keeps the addition order of this map.
    Parameters:
      `index` - the position to fetch
    Returns:
      the value at this position
    Throws:
      `IndexOutOfBoundsException` - when the ask for the position is invalid

  - ### remove

    public 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.
    Parameters:
      `index` - the position to remove the data from
    Returns:
      the value stored
    Throws:
      `IndexOutOfBoundsException` - when the ask for the position is invalid

  - ### put

    public V put(K key, V value)
    Specified by:
      `put` in interface `Map<K,V>`

  - ### addFirst

    public V addFirst(K key, V value)

  - ### add

    public V add(K key, V value)

  - ### addLast

    public V addLast(K key, V value)

  - ### getFirst

    public V getFirst()

  - ### getLast

    public V getLast()

  - ### removeFirst

    public V removeFirst()

  - ### removeLast

    public V removeLast()

  - ### containsKey

    public boolean containsKey(Object key)
    Checks of a key is in the map.
    Specified by:
      `containsKey` in interface `Map<K,V>`
    Parameters:
      `key` - the key to check
    Returns:
      true of the key is in the map, false otherwise

  - ### containsValue

    public boolean containsValue(Object value)
    Specified by:
      `containsValue` in interface `Map<K,V>`

  - ### isEmpty

    public boolean isEmpty()
    Specified by:
      `isEmpty` in interface `Map<K,V>`

  - ### entrySet

    public Set<Map.Entry<K,V>> entrySet()
    Specified by:
      `entrySet` in interface `Map<K,V>`

  - ### keySet

    public Set<K> keySet()
    Specified by:
      `keySet` in interface `Map<K,V>`

  - ### reverse

    public void reverse()
    Just reverses the ordering of the map as created so far.

  - ### putAll

    public void putAll(Map<? extends K,? extends V> src)
    Specified by:
      `putAll` in interface `Map<K,V>`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`