Class HashMap<T,K,V>
java.lang.Object
org.javimmutable.collections.common.AbstractMap<K,V>
org.javimmutable.collections.hash.HashMap<T,K,V>
- All Implemented Interfaces:
Serializable
,Iterable<IMapEntry<K,
,V>> ArrayAssignMapper<K,
,V, ArrayMapNode<K, V>> ArrayDeleteMapper<K,
,ArrayMapNode<K, V>> ArrayFindEntryMapper<K,
,V, ArrayMapNode<K, V>> ArrayGetMapper<K,
,V, ArrayMapNode<K, V>> ArrayIterationMapper<K,
,V, ArrayMapNode<K, V>> ArraySizeMapper<ArrayMapNode<K,
,V>> ArrayUpdateMapper<K,
,V, ArrayMapNode<K, V>> ICollection<IMapEntry<K,
,V>> IMap<K,
,V> InvariantCheckable
,IStreamable<IMapEntry<K,
,V>> Mapped<K,
,V> SplitableIterable<IMapEntry<K,
V>>
@Immutable
public class HashMap<T,K,V>
extends AbstractMap<K,V>
implements ArrayUpdateMapper<K,V,ArrayMapNode<K,V>>, ArrayFindEntryMapper<K,V,ArrayMapNode<K,V>>, ArrayIterationMapper<K,V,ArrayMapNode<K,V>>, ArrayDeleteMapper<K,ArrayMapNode<K,V>>, Serializable
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionSets the value associated with a specific key.static <K,
V> IMapBuilder <K, V> builder()
void
Checks invariants of implementing class.Deletes the entry for the specified key (if any).Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself.Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself.void
Processes every key/value pair in this map using the provided function.<E extends Exception>
voidforEachThrows
(Proc2Throws<K, V, E> proc) Processes every key/value pair in this map using the provided function.static <K,
V> IMap <K, V> forKey
(K key) Returns an empty map using the appropriate collision handling strategy for the given key's class.getValueOr
(K key, V defaultValue) Return the value associated with key or defaultValue if no value is associated.iterator()
Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator.Creates a Builder with the same type signature as this Map.mappedAssign
(K key, V value) Called during assign operation to create a new mapping for the given key and value.mappedAssign
(ArrayMapNode<K, V> current, K key, V value) Called during assign operation to replace an existing mapping for the given key and value.mappedDelete
(ArrayMapNode<K, V> current, K key) Called during delete operation to delete a key from a mapping.mappedEntries
(ArrayMapNode<K, V> mapping) mappedFind
(ArrayMapNode<K, V> mapping, K key) mappedFindEntry
(ArrayMapNode<K, V> mapping, K key) mappedGetValueOr
(ArrayMapNode<K, V> mapping, K key, V defaultValue) mappedKeys
(ArrayMapNode<K, V> mapping) int
mappedSize
(ArrayMapNode<K, V> mapping) Called to obtain number of keys in a given mapping.mappedUpdate
(ArrayMapNode<K, V> current, K key, Func1<Maybe<V>, V> generator) mappedValues
(ArrayMapNode<K, V> mapping) static <K,
V> EmptyHashMap <K, V> of()
Returns an empty hash map.static <K,
V> IMap <K, V> Returns an empty map using the appropriate collision handling strategy for keys of the given class.<R> R
Processes every key value pair in this map using the provided function to produce a value.<R,
E extends Exception>
RreduceThrows
(R startingSum, Sum2Throws<K, V, R, E> proc) Processes every key value pair in this map using the provided function to produce a value.int
size()
Update the value at the key.static <K,
V> IMap <K, V> Returns an empty map using linked lists for handling hash code collisions.static <K extends Comparable<K>,
V>
IMap<K, V> Returns an empty map using linked lists for handling hash code collisions.Methods inherited from class org.javimmutable.collections.common.AbstractMap
assignAll, assignAll, equals, get, getMap, getSpliteratorCharacteristics, hashCode, insert, keys, reject, select, toString, values
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.javimmutable.collections.ICollection
isEmpty, isNonEmpty
Methods inherited from interface org.javimmutable.collections.IMap
insertAll, insertAll, mapCollector
Methods inherited from interface org.javimmutable.collections.IStreamable
parallelStream, spliterator, stream
Methods inherited from interface org.javimmutable.collections.SplitableIterable
forEachThrows, indexedForEach, indexedForEachThrows, reduce, reduceThrows
-
Method Details
-
of
Returns an empty hash map. The empty map will automatically select a collision handling strategy on the first call to assign() based on the key for that call. For this reason all keys used for a given map must either implement or not implement Comparable. If some keys implement it and some do not the collision handling code will likely fail due to a class cast exception or a method not defined exception. -
of
Returns an empty map using the appropriate collision handling strategy for keys of the given class. All keys used with that map should derive from the specified class to avoid runtime problems with incompatible keys. -
forKey
Returns an empty map using the appropriate collision handling strategy for the given key's class. All keys used with that map should derive from the specified key's class to avoid runtime problems with incompatible keys. -
usingList
Returns an empty map using linked lists for handling hash code collisions. This is safe for any type of key but is slower when many keys have the same hash code. -
usingTree
Returns an empty map using linked lists for handling hash code collisions. This is faster than the list based collision handling but depends on all keys implementing Comparable and being able to compare themselves to all other keys. -
builder
-
mapBuilder
Description copied from interface:IMap
Creates a Builder with the same type signature as this Map.- Specified by:
mapBuilder
in interfaceIMap<T,
K>
-
createMapCollector
-
getValueOr
Description copied from interface:Mapped
Return the value associated with key or defaultValue if no value is associated. Note that if defaultValue is an acceptable value to the container then this method will be ambiguous and find() should be used instead.- Specified by:
getValueOr
in interfaceMapped<T,
K> - Parameters:
key
- identifies the value to retrievedefaultValue
- value to return if no entry exists for key- Returns:
- value associated with key or defaultValue if no value is associated
-
find
Description copied from interface:IMap
Search for a value within the map and return a Holder indicating if the value was found and, if it was found, the value itself. Holder allows null values to be returned unambiguously. -
findEntry
Description copied from interface:IMap
Search for an Entry within the map and return a Holder indicating if the Entry was found and, if it was found, the Entry itself. -
assign
Description copied from interface:IMap
Sets the value associated with a specific key. Key must be non-null but value can be null. If the key already has a value in the map the old value is discarded and the new value is stored in its place. Returns a newIMap
reflecting any changes. The original map is always left unchanged. -
update
Description copied from interface:IMap
Update the value at the key. A Holder containing the value currently stored at the key, or an empty Holder if the key is not currently bound, is passed to the generator function. -
delete
Description copied from interface:IMap
Deletes the entry for the specified key (if any). Returns a new map if the value was deleted or the current map if the key was not contained in the map. -
size
public int size()- Specified by:
size
in interfaceICollection<T>
- Returns:
- number of values in the collection
-
deleteAll
-
iterator
Description copied from interface:IStreamable
Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator. This is necessary to allow composition of new objects from methods like keys() and values().- Specified by:
iterator
in interfaceIStreamable<T>
- Specified by:
iterator
in interfaceIterable<T>
- Specified by:
iterator
in interfaceSplitableIterable<T>
-
forEach
Description copied from interface:IMap
Processes every key/value pair in this map using the provided function. -
forEachThrows
Description copied from interface:IMap
Processes every key/value pair in this map using the provided function.- Specified by:
forEachThrows
in interfaceIMap<T,
K> - Throws:
E
-
reduce
Description copied from interface:IMap
Processes every key value pair in this map using the provided function to produce a value.- Specified by:
reduce
in interfaceIMap<T,
K> - Type Parameters:
R
- type of the sum- Parameters:
startingSum
- initial value for process (used with first key/value pair of map)proc
- function to combine a sum with a key value pair to produce a new sum- Returns:
- final value (or initial value if this map is empty)
-
reduceThrows
public <R,E extends Exception> R reduceThrows(R startingSum, @Nonnull Sum2Throws<K, V, throws ER, E> proc) Description copied from interface:IMap
Processes every key value pair in this map using the provided function to produce a value.- Specified by:
reduceThrows
in interfaceIMap<T,
K> - Type Parameters:
R
- type of the sumE
- type of the Exception thrown by the function- Parameters:
startingSum
- initial value for process (used with first key/value pair of map)proc
- function to combine a sum with a key value pair to produce a new sum- Returns:
- final value (or initial value if this map is empty)
- Throws:
E
-
checkInvariants
public void checkInvariants()Description copied from interface:InvariantCheckable
Checks invariants of implementing class.- Specified by:
checkInvariants
in interfaceInvariantCheckable
-
mappedGetValueOr
- Specified by:
mappedGetValueOr
in interfaceArrayGetMapper<T,
K, V>
-
mappedFind
- Specified by:
mappedFind
in interfaceArrayGetMapper<T,
K, V>
-
mappedFindEntry
@Nonnull public Maybe<IMapEntry<K,V>> mappedFindEntry(@Nonnull ArrayMapNode<K, V> mapping, @Nonnull K key) - Specified by:
mappedFindEntry
in interfaceArrayFindEntryMapper<T,
K, V>
-
mappedAssign
Description copied from interface:ArrayAssignMapper
Called during assign operation to create a new mapping for the given key and value.- Specified by:
mappedAssign
in interfaceArrayAssignMapper<T,
K, V> - Parameters:
key
- key being assigned tovalue
- value being assigned- Returns:
- non-null mapping
-
mappedAssign
@Nonnull public ArrayMapNode<K,V> mappedAssign(@Nonnull ArrayMapNode<K, V> current, @Nonnull K key, V value) Description copied from interface:ArrayAssignMapper
Called during assign operation to replace an existing mapping for the given key and value.- Specified by:
mappedAssign
in interfaceArrayAssignMapper<T,
K, V> - Parameters:
current
- mapping to be replacedkey
- key being assigned tovalue
- value being assigned- Returns:
- same to keep mapping or non-null to replace mapping
-
mappedUpdate
@Nonnull public ArrayMapNode<K,V> mappedUpdate(@Nonnull ArrayMapNode<K, V> current, @Nonnull K key, @Nonnull Func1<Maybe<V>, V> generator) - Specified by:
mappedUpdate
in interfaceArrayUpdateMapper<T,
K, V>
-
mappedDelete
Description copied from interface:ArrayDeleteMapper
Called during delete operation to delete a key from a mapping.- Specified by:
mappedDelete
in interfaceArrayDeleteMapper<T,
K> - Parameters:
current
- mapping to be replacedkey
- key being deleted- Returns:
- null to remove mapping, same to keep mapping, or non-null to replace mapping
-
mappedSize
Description copied from interface:ArraySizeMapper
Called to obtain number of keys in a given mapping.- Specified by:
mappedSize
in interfaceArraySizeMapper<T>
- Parameters:
mapping
- mapping to be sized- Returns:
- number of keys in the mapping
-
mappedKeys
- Specified by:
mappedKeys
in interfaceArrayIterationMapper<T,
K, V>
-
mappedValues
- Specified by:
mappedValues
in interfaceArrayIterationMapper<T,
K, V>
-
mappedEntries
@Nonnull public GenericIterator.Iterable<IMapEntry<K,V>> mappedEntries(@Nonnull ArrayMapNode<K, V> mapping) - Specified by:
mappedEntries
in interfaceArrayIterationMapper<T,
K, V>
-