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.
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 Summary
ConstructorsConstructorDescriptionCreates a new and emptyOrderedHashMap.OrderedHashMap(OrderedHashMap<K, V> map) Creates a newOrderedHashMapthat contains all the mappings of the specified map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from this map.booleancontainsKey(Object key) Returns if the specified key is present in this map.booleanvoidReturns the value that is mapped to the specified key.getFirst()Returns the first entry in this ordered map.getLast()Returns the last entry in this ordered map.inthashCode()booleanisEmpty()Returns if this map is empty.iterator()Returns anIteratorthat iterates over the entries in this map in the order of their first insertion.Associates the specified key with the specified value.voidputAll(OrderedHashMap<K, V> otherMap) Inserts all mappings of the specified map into this map.putIfAbsent(K key, V value) Associates the specified key with the specified value and adds the entry to the end of the list.Removes the entry with the specified key from this map.Removes and returns the first entry in this ordered map.Removes and returns the last entry in this ordered map.longsize()Returns the number of entries in this map.Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
spliterator
-
Constructor Details
-
OrderedHashMap
public OrderedHashMap()Creates a new and emptyOrderedHashMap. -
OrderedHashMap
Creates a newOrderedHashMapthat contains all the mappings of the specified map.- Parameters:
map- The mappings that should be cloned. May benull.
-
-
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
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. Notnull.value- The value of the new mapping. Notnull.- Returns:
- The value that was previously mapped to the key.
If the key was not present in this map,
nullis returned. - Throws:
IllegalArgumentException- If the key or the value isnull.
-
putIfAbsent
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. Notnull.value- The value of the new mapping. Notnull.- 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 isnull.
-
putAll
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 isnullor this map, nothing happens.- Parameters:
otherMap- The map whose mappings should be put into this map. May benull.
-
remove
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 benull.- Returns:
- The value of the entry that was removed.
If no entry was removed,
nullis returned.
-
clear
public void clear()Removes all entries from this map. -
get
-
containsKey
Returns if the specified key is present in this map. This method is semantically equivalent toget(key) != null
- Parameters:
key- The key that should be checked. May benull.- Returns:
- If the specified key is present in this map.
-
forEach
-
iterator
-
getFirst
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
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
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
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
-
hashCode
-