Class OrderedHashMap<K,V>

java.lang.Object
de.tomatengames.util.map.OrderedHashMap<K,V>
Type Parameters:
K - The type of the keys.
V - The type of the values.
All Implemented Interfaces:
Iterable<Map.Entry<K,V>>

public final class OrderedHashMap<K,V> extends Object implements Iterable<Map.Entry<K,V>>
A HashMap-like data structure that maps keys to values. Equality of keys is checked by using the Object.equals(Object) method.

This map manages a doubly-linked list to store the entries in the order of insertion and a hash table to speed up lookups. The Iterator of this map iterates the entries in their original order of insertion.

This map does not allow null keys and values. This implementation does not allow concurrent modifications.

Since:
1.7
  • Constructor Details

    • OrderedHashMap

      public OrderedHashMap()
      Creates a new and empty OrderedHashMap.
    • OrderedHashMap

      public OrderedHashMap(OrderedHashMap<K,V> map)
      Creates a new OrderedHashMap that contains all the mappings of the specified map.
      Parameters:
      map - The mappings that should be cloned. May be null.
  • Method Details

    • size

      public long size()
      Returns the number of entries in this map.
      Returns:
      The number of entries.
    • isEmpty

      public boolean isEmpty()
      Returns if this map is empty.
      Returns:
      If this map has no entries.
    • put

      public V put(K key, V value)
      Associates the specified key with the specified value. If the key is not present in this map, a new entry is added to the end of the list. If the key is already present in this map, the value of the previous entry is replaced.
      Parameters:
      key - The key of the new mapping. Not null.
      value - The value of the new mapping. Not null.
      Returns:
      The value that was previously mapped to the key. If the key was not present in this map, null is returned.
      Throws:
      IllegalArgumentException - If the key or the value is null.
    • putIfAbsent

      public V putIfAbsent(K key, V value)
      Associates the specified key with the specified value and adds the entry to the end of the list. If the key is already present in this map, nothing happens.
      Parameters:
      key - The key of the new mapping. Not null.
      value - The value of the new mapping. Not null.
      Returns:
      The value that was previously mapped to the key. If null, the new value has been put into the map. Otherwise, the previous value is still present.
      Throws:
      IllegalArgumentException - If the key or the value is null.
    • putAll

      public void putAll(OrderedHashMap<K,V> otherMap)
      Inserts all mappings of the specified map into this map. Keys that are present in both maps are replaced in this map. If the specified map is null or this map, nothing happens.
      Parameters:
      otherMap - The map whose mappings should be put into this map. May be null.
    • remove

      public V remove(Object key)
      Removes the entry with the specified key from this map. If the map does not contain the key, nothing happens.
      Parameters:
      key - The key of the entry that should be removed. May be null.
      Returns:
      The value of the entry that was removed. If no entry was removed, null is returned.
    • clear

      public void clear()
      Removes all entries from this map.
    • get

      public V get(K key)
      Returns the value that is mapped to the specified key.
      Parameters:
      key - The key whose value should be returned. May be null.
      Returns:
      The value that the specified key is mapped to. If this map does not contain the key, null is returned.
    • containsKey

      public boolean containsKey(Object key)
      Returns if the specified key is present in this map. This method is semantically equivalent to
      get(key) != null
      Parameters:
      key - The key that should be checked. May be null.
      Returns:
      If the specified key is present in this map.
    • forEach

      public void forEach(Consumer<? super Map.Entry<K,V>> action)
      Specified by:
      forEach in interface Iterable<K>
    • iterator

      public Iterator<Map.Entry<K,V>> iterator()
      Returns an Iterator that iterates over the entries in this map in the order of their first insertion.
      Specified by:
      iterator in interface Iterable<K>
    • getFirst

      public Map.Entry<K,V> getFirst() throws NoSuchElementException
      Returns the first entry in this ordered map.
      Returns:
      the first entry in this ordered map
      Throws:
      NoSuchElementException - if this map is empty
      Since:
      1.8
    • getLast

      public Map.Entry<K,V> getLast() throws NoSuchElementException
      Returns the last entry in this ordered map.
      Returns:
      the last entry in this ordered map
      Throws:
      NoSuchElementException - if this map is empty
      Since:
      1.8
    • removeFirst

      public Map.Entry<K,V> removeFirst() throws NoSuchElementException
      Removes and returns the first entry in this ordered map.
      Returns:
      the removed entry
      Throws:
      NoSuchElementException - if this map is empty
      Since:
      1.8
    • removeLast

      public Map.Entry<K,V> removeLast() throws NoSuchElementException
      Removes and returns the last entry in this ordered map.
      Returns:
      the removed entry
      Throws:
      NoSuchElementException - if this map is empty
      Since:
      1.8
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object