Class AbstractHashMap<K,V>
java.lang.Object
de.tomatengames.util.map.AbstractHashMap<K,V>
- Type Parameters:
K- The key type.V- The type of the values.
- All Implemented Interfaces:
Iterable<AbstractEntry<K,V>>
A
HashMap-like data structure that maps K keys to object values.
Equality of keys is checked by using the abstract keyEquals(key1, key2) method.
This map does not allow null values.
This implementation does not allow concurrent modifications.
- Since:
- 1.5
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new and emptyAbstractHashMap.AbstractHashMap(AbstractHashMap<K, V> map) Creates a newAbstractHashMapthat contains all the mappings of the specified map. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all entries from this map.booleancontainsKey(K key) Returns if the specified key is present in this map.booleanvoidforEach(Consumer<? super AbstractEntry<K, V>> action) Returns the value that is mapped to the specified key.inthashCode()booleanisEmpty()Returns if this map does not contain any entry.iterator()abstract booleanReturns if the specified keys should be considered equal.abstract intCalculate the hash code of the specified key.Associates the specified key with the specified value.voidputAll(AbstractHashMap<K, V> otherMap) Puts all mappings of the specified map into this map.putIfAbsent(K key, V value) Associates the specified key with the specified value.Removes the entry with the specified key from this 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
-
AbstractHashMap
public AbstractHashMap()Creates a new and emptyAbstractHashMap. -
AbstractHashMap
Creates a newAbstractHashMapthat contains all the mappings of the specified map.- Parameters:
map- The mappings that should be cloned.
-
-
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 does not contain any entry.- Returns:
- If this map has no entries.
-
put
Associates the specified key with the specified value. If the key is already present in this map, the previous value is replaced.- Parameters:
key- The key of the new mapping.value- The value of the new mapping. Must 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 value isnull.
-
putIfAbsent
Associates the specified key with the specified value. If the key is already present in this map, nothing happens.- Parameters:
key- The key of the new mapping.value- The value of the new mapping. Must 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 value isnull.
-
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.- Returns:
- If the specified key is present in this map.
-
remove
-
clear
public void clear()Removes all entries from this map. -
forEach
-
putAll
Puts 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.
-
equals
-
hashCode
-
keyHash
Calculate the hash code of the specified key.- Parameters:
key- The key whose hash code should be calculated.- Returns:
- The hash code.
-
keyEquals
-
iterator
-