K - the key typeV - the value typepublic final class HashMap<K,V> extends java.lang.Object implements Map<K,V>, java.io.Serializable
Features:
Performance characteristics:
Implementation details:
This map performs read and write operations of single elements in O(1) time, and in O(1) space.
The CHAMP trie contains nodes that may be shared with other maps.
If a write operation is performed on a node, then this map creates a copy of the node and of all parent nodes up to the root (copy-path-on-write). Since the CHAMP trie has a fixed maximal height, the cost is O(1).
All operations on this map can be performed concurrently, without a need for synchronisation.
The immutable version of this map extends from the non-public class
ChampBitmapIndexNode. This design safes 16 bytes for every instance,
and reduces the number of redirections for finding an element in the
collection by 1.
References:
Portions of the code in this class have been derived from 'The Capsule Hash Trie Collections Library', and from 'JHotDraw 8'.
| Modifier and Type | Method and Description |
|---|---|
<K2,V2> HashMap<K2,V2> |
bimap(java.util.function.Function<? super K,? extends K2> keyMapper,
java.util.function.Function<? super V,? extends V2> valueMapper)
Maps this
Map to a new Map with different component type by applying a function to its elements. |
static <K,V> java.util.stream.Collector<Tuple2<K,V>,java.util.ArrayList<Tuple2<K,V>>,HashMap<K,V>> |
collector()
Returns a
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap. |
static <K,V,T extends V> |
collector(java.util.function.Function<? super T,? extends K> keyMapper)
Returns a
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap. |
static <K,V,T> java.util.stream.Collector<T,java.util.ArrayList<T>,HashMap<K,V>> |
collector(java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends V> valueMapper)
Returns a
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap. |
Tuple2<V,HashMap<K,V>> |
computeIfAbsent(K key,
java.util.function.Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value,
attempts to compute its value using the given mapping
function and enters it into this map.
|
Tuple2<Option<V>,HashMap<K,V>> |
computeIfPresent(K key,
java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present, attempts to
compute a new mapping given the key and its current mapped value.
|
boolean |
containsKey(K key)
Returns
true if this map contains a mapping for the specified key. |
HashMap<K,V> |
distinct()
Returns a new version of this which contains no duplicates.
|
HashMap<K,V> |
distinctBy(java.util.Comparator<? super Tuple2<K,V>> comparator)
Returns a new version of this which contains no duplicates.
|
<U> HashMap<K,V> |
distinctBy(java.util.function.Function<? super Tuple2<K,V>,? extends U> keyExtractor)
Returns a new version of this which contains no duplicates.
|
HashMap<K,V> |
drop(int n)
Drops the first n elements of this or all elements, if this length < n.
|
HashMap<K,V> |
dropRight(int n)
Drops the last n elements of this or all elements, if this length < n.
|
HashMap<K,V> |
dropUntil(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Drops elements until the predicate holds for the current element.
|
HashMap<K,V> |
dropWhile(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Drops elements while the predicate holds for the current element.
|
static <K,V> HashMap<K,V> |
empty() |
boolean |
equals(java.lang.Object other)
In Vavr there are four basic classes of collections:
Seq (sequential elements)
Set (distinct elements)
Map (indexed elements)
Multimap (indexed collections)
Two collection instances of these classes are equal if and only if both collections
belong to the same basic collection class (Seq, Set, Map or Multimap)
contain the same elements
have the same element order, if the collections are of type Seq
Two Map/Multimap elements, resp.
|
static <K,V> HashMap<K,V> |
fill(int n,
java.util.function.Supplier<? extends Tuple2<? extends K,? extends V>> s)
Returns a HashMap containing tuples returned by
n calls to a given Supplier s. |
HashMap<K,V> |
filter(java.util.function.BiPredicate<? super K,? super V> predicate)
Returns a new Map consisting of all elements which satisfy the given predicate.
|
HashMap<K,V> |
filter(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Returns a new traversable consisting of all elements which satisfy the given predicate.
|
HashMap<K,V> |
filterKeys(java.util.function.Predicate<? super K> predicate)
Returns a new Map consisting of all elements with keys which satisfy the given predicate.
|
HashMap<K,V> |
filterNot(java.util.function.BiPredicate<? super K,? super V> predicate)
Returns a new Map consisting of all elements which do not satisfy the given predicate.
|
HashMap<K,V> |
filterNot(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Returns a new traversable consisting of all elements which do not satisfy the given predicate.
|
HashMap<K,V> |
filterNotKeys(java.util.function.Predicate<? super K> predicate)
Returns a new Map consisting of all elements with keys which do not satisfy the given predicate.
|
HashMap<K,V> |
filterNotValues(java.util.function.Predicate<? super V> predicate)
Returns a new Map consisting of all elements with values which do not satisfy the given predicate.
|
HashMap<K,V> |
filterValues(java.util.function.Predicate<? super V> predicate)
Returns a new Map consisting of all elements with values which satisfy the given predicate.
|
<K2,V2> HashMap<K2,V2> |
flatMap(java.util.function.BiFunction<? super K,? super V,? extends java.lang.Iterable<Tuple2<K2,V2>>> mapper)
FlatMaps this
Map to a new Map with different component type. |
Option<V> |
get(K key)
Returns the
Some of value to which the specified key
is mapped, or None if this map contains no mapping for the key. |
V |
getOrElse(K key,
V defaultValue)
Returns the value associated with a key, or a default value if the key is not contained in the map.
|
<C> Map<C,HashMap<K,V>> |
groupBy(java.util.function.Function<? super Tuple2<K,V>,? extends C> classifier)
Groups this elements by classifying the elements.
|
Iterator<HashMap<K,V>> |
grouped(int size)
Groups this
Traversable into fixed size blocks. |
int |
hashCode()
Returns the hash code of this collection.
|
Tuple2<K,V> |
head()
Returns the first element of a non-empty Traversable.
|
HashMap<K,V> |
init()
XXX We return tail() here.
|
Option<HashMap<K,V>> |
initOption()
Dual of Traversable.tailOption(), returning all elements except the last as
Option. |
boolean |
isAsync()
A
HashMap is computed synchronously. |
boolean |
isEmpty()
Checks if this Traversable is empty.
|
boolean |
isLazy()
A
HashMap is computed eagerly. |
Iterator<Tuple2<K,V>> |
iterator()
An iterator by means of head() and tail().
|
Set<K> |
keySet()
Returns the keys contained in this map.
|
Iterator<K> |
keysIterator()
Returns the keys contained in this map as an iterator.
|
Tuple2<K,V> |
last()
Dual of Traversable.head(), returning the last element.
|
<K2,V2> HashMap<K2,V2> |
map(java.util.function.BiFunction<? super K,? super V,Tuple2<K2,V2>> mapper)
Maps the entries of this
Map to form a new Map. |
<K2> HashMap<K2,V> |
mapKeys(java.util.function.Function<? super K,? extends K2> keyMapper)
Maps the keys of this
Map while preserving the corresponding values. |
<K2> HashMap<K2,V> |
mapKeys(java.util.function.Function<? super K,? extends K2> keyMapper,
java.util.function.BiFunction<? super V,? super V,? extends V> valueMerge)
Maps the keys of this
Map while preserving the corresponding values and applying a value merge function on collisions. |
<V2> HashMap<K,V2> |
mapValues(java.util.function.Function<? super V,? extends V2> valueMapper)
Maps the values of this
Map while preserving the corresponding keys. |
HashMap<K,V> |
merge(Map<? extends K,? extends V> that)
Creates a new map which by merging the entries of
this map and that map. |
<U extends V> |
merge(Map<? extends K,U> that,
java.util.function.BiFunction<? super V,? super U,? extends V> collisionResolution)
Creates a new map which by merging the entries of
this map and that map. |
static <K,V> HashMap<K,V> |
narrow(HashMap<? extends K,? extends V> hashMap)
Narrows a widened
HashMap<? extends K, ? extends V> to HashMap<K, V>
by performing a type-safe cast. |
static <K,V> HashMap<K,V> |
of(K key,
V value)
Returns a singleton
HashMap, i.e. |
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5,
K k6,
V v6)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5,
K k6,
V v6,
K k7,
V v7)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5,
K k6,
V v6,
K k7,
V v7,
K k8,
V v8)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5,
K k6,
V v6,
K k7,
V v7,
K k8,
V v8,
K k9,
V v9)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(K k1,
V v1,
K k2,
V v2,
K k3,
V v3,
K k4,
V v4,
K k5,
V v5,
K k6,
V v6,
K k7,
V v7,
K k8,
V v8,
K k9,
V v9,
K k10,
V v10)
Creates a HashMap of the given list of key-value pairs.
|
static <K,V> HashMap<K,V> |
of(Tuple2<? extends K,? extends V> entry)
Returns a singleton
HashMap, i.e. |
static <K,V> HashMap<K,V> |
ofAll(java.util.Map<? extends K,? extends V> map)
Returns a
HashMap, from a source java.util.Map. |
static <T,K,V> HashMap<K,V> |
ofAll(java.util.stream.Stream<? extends T> stream,
java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends V> valueMapper)
Returns a
HashMap, from entries mapped from stream. |
static <T,K,V> HashMap<K,V> |
ofAll(java.util.stream.Stream<? extends T> stream,
java.util.function.Function<? super T,Tuple2<? extends K,? extends V>> entryMapper)
Returns a
HashMap, from entries mapped from stream. |
static <K,V> HashMap<K,V> |
ofEntries(java.lang.Iterable<? extends Tuple2<? extends K,? extends V>> entries)
Creates a HashMap of the given entries.
|
static <K,V> HashMap<K,V> |
ofEntries(java.util.Map.Entry<? extends K,? extends V>... entries)
Creates a HashMap of the given entries.
|
static <K,V> HashMap<K,V> |
ofEntries(Tuple2<? extends K,? extends V>... entries)
Creates a HashMap of the given entries.
|
HashMap<K,V> |
orElse(java.lang.Iterable<? extends Tuple2<K,V>> other)
Returns this
Traversable if it is nonempty, otherwise return the alternative. |
HashMap<K,V> |
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends Tuple2<K,V>>> supplier)
Returns this
Traversable if it is nonempty, otherwise return the result of evaluating supplier. |
Tuple2<HashMap<K,V>,HashMap<K,V>> |
partition(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Creates a partition of this
Traversable by splitting this elements in two in distinct traversables
according to a predicate. |
HashMap<K,V> |
peek(java.util.function.Consumer<? super Tuple2<K,V>> action)
Performs the given
action on the first element if this is an eager implementation. |
<U extends V> |
put(K key,
U value,
java.util.function.BiFunction<? super V,? super U,? extends V> merge)
Associates the specified value with the specified key in this map.
|
HashMap<K,V> |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
HashMap<K,V> |
put(Tuple2<? extends K,? extends V> entry)
Convenience method for
put(entry._1, entry._2). |
<U extends V> |
put(Tuple2<? extends K,U> entry,
java.util.function.BiFunction<? super V,? super U,? extends V> merge)
Convenience method for
put(entry._1, entry._2, merge). |
HashMap<K,V> |
remove(K key)
Removes the mapping for a key from this map if it is present.
|
HashMap<K,V> |
removeAll(java.lang.Iterable<? extends K> c)
Removes the mapping for a key from this map if it is present.
|
HashMap<K,V> |
replace(K key,
V oldValue,
V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
|
HashMap<K,V> |
replace(Tuple2<K,V> currentElement,
Tuple2<K,V> newElement)
Replaces the first occurrence (if exists) of the given currentElement with newElement.
|
HashMap<K,V> |
replaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)
Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
|
HashMap<K,V> |
replaceAll(Tuple2<K,V> currentElement,
Tuple2<K,V> newElement)
Replaces all occurrences of the given currentElement with newElement.
|
HashMap<K,V> |
replaceValue(K key,
V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
|
HashMap<K,V> |
retainAll(java.lang.Iterable<? extends Tuple2<K,V>> elements)
Keeps all occurrences of the given elements from this.
|
HashMap<K,V> |
scan(Tuple2<K,V> zero,
java.util.function.BiFunction<? super Tuple2<K,V>,? super Tuple2<K,V>,? extends Tuple2<K,V>> operation)
Computes a prefix scan of the elements of the collection.
|
int |
size()
Computes the number of elements of this Traversable.
|
Iterator<HashMap<K,V>> |
slideBy(java.util.function.Function<? super Tuple2<K,V>,?> classifier)
Slides a non-overlapping window of a variable size over this
Traversable. |
Iterator<HashMap<K,V>> |
sliding(int size)
Slides a window of a specific
size and step size 1 over this Traversable by calling
Traversable.sliding(int, int). |
Iterator<HashMap<K,V>> |
sliding(int size,
int step)
Slides a window of a specific
size and step size over this Traversable. |
Tuple2<HashMap<K,V>,HashMap<K,V>> |
span(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Returns a tuple where the first element is the longest prefix of elements that satisfy the given
predicate and the second element is the remainder. |
java.util.Spliterator<Tuple2<K,V>> |
spliterator() |
java.lang.String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
static <K,V> HashMap<K,V> |
tabulate(int n,
java.util.function.Function<? super java.lang.Integer,? extends Tuple2<? extends K,? extends V>> f)
Returns an HashMap containing
n values of a given Function f
over a range of integer values from 0 to n - 1. |
HashMap<K,V> |
tail()
Drops the first element of a non-empty Traversable.
|
Option<HashMap<K,V>> |
tailOption()
Drops the first element of a non-empty Traversable and returns an
Option. |
HashMap<K,V> |
take(int n)
Takes the first n elements of this or all elements, if this length < n.
|
HashMap<K,V> |
takeRight(int n)
Takes the last n elements of this or all elements, if this length < n.
|
HashMap<K,V> |
takeUntil(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Takes elements until the predicate holds for the current element.
|
HashMap<K,V> |
takeWhile(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Takes elements while the predicate holds for the current element.
|
java.util.HashMap<K,V> |
toJavaMap()
Converts this Vavr
Map to a java.util.Map while preserving characteristics
like insertion order (LinkedHashMap) and sort order (SortedMap). |
java.lang.String |
toString()
Clarifies that values have a proper toString() method implemented.
|
Stream<V> |
values()
Returns a new
Seq that contains the values of this Map. |
Iterator<V> |
valuesIterator()
Returns the values in this map.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitapply, asPartialFunction, collect, contains, containsValue, entry, flatMap, foldRight, forEach, hasDefiniteSize, isDefinedAt, isDistinct, isTraversableAgain, iterator, length, lift, map, narrow, scanLeft, scanRight, transform, unzip, zip, zipAll, zipWith, zipWithIndex, zipWithIndexarrangeBy, average, containsAll, count, existsUnique, find, findLast, fold, foldLeft, forEachWithIndex, get, headOption, isOrdered, isSequential, isSingleValued, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, narrow, nonEmpty, product, reduce, reduceLeft, reduceLeftOption, reduceOption, reduceRight, reduceRightOption, single, singleOption, sum, toLinkedMap, toMap, toSortedMap, toSortedMap, unzip, unzip3collect, collect, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, narrow, out, out, stderr, stdout, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVectorpublic static <K,V> java.util.stream.Collector<Tuple2<K,V>,java.util.ArrayList<Tuple2<K,V>>,HashMap<K,V>> collector()
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap.K - The key typeV - The value typeHashMap Collector.public static <K,V,T extends V> java.util.stream.Collector<T,java.util.ArrayList<T>,HashMap<K,V>> collector(java.util.function.Function<? super T,? extends K> keyMapper)
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap.K - The key typeV - The value typeT - Initial Stream elements typekeyMapper - The key mapperHashMap Collector.public static <K,V,T> java.util.stream.Collector<T,java.util.ArrayList<T>,HashMap<K,V>> collector(java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
Collector which may be used in conjunction with
Stream.collect(java.util.stream.Collector) to obtain a HashMap.K - The key typeV - The value typeT - Initial Stream elements typekeyMapper - The key mappervalueMapper - The value mapperHashMap Collector.public static <K,V> HashMap<K,V> empty()
public static <K,V> HashMap<K,V> narrow(HashMap<? extends K,? extends V> hashMap)
HashMap<? extends K, ? extends V> to HashMap<K, V>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.K - Key typeV - Value typehashMap - A HashMap.hashMap instance as narrowed type HashMap<K, V>.public static <K,V> HashMap<K,V> of(Tuple2<? extends K,? extends V> entry)
HashMap, i.e. a HashMap of one element.K - The key typeV - The value typeentry - A map entry.public static <K,V> HashMap<K,V> ofAll(java.util.Map<? extends K,? extends V> map)
HashMap, from a source java.util.Map.K - The key typeV - The value typemap - A mappublic static <T,K,V> HashMap<K,V> ofAll(java.util.stream.Stream<? extends T> stream, java.util.function.Function<? super T,? extends K> keyMapper, java.util.function.Function<? super T,? extends V> valueMapper)
HashMap, from entries mapped from stream.T - The stream element typeK - The key typeV - The value typestream - the source streamkeyMapper - the key mappervalueMapper - the value mapperpublic static <T,K,V> HashMap<K,V> ofAll(java.util.stream.Stream<? extends T> stream, java.util.function.Function<? super T,Tuple2<? extends K,? extends V>> entryMapper)
HashMap, from entries mapped from stream.T - The stream element typeK - The key typeV - The value typestream - the source streamentryMapper - the entry mapperpublic static <K,V> HashMap<K,V> of(K key, V value)
HashMap, i.e. a HashMap of one element.K - The key typeV - The value typekey - A singleton map key.value - A singleton map value.public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5k6 - a key for the mapv6 - the value for k6public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5k6 - a key for the mapv6 - the value for k6k7 - a key for the mapv7 - the value for k7public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5k6 - a key for the mapv6 - the value for k6k7 - a key for the mapv7 - the value for k7k8 - a key for the mapv8 - the value for k8public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5k6 - a key for the mapv6 - the value for k6k7 - a key for the mapv7 - the value for k7k8 - a key for the mapv8 - the value for k8k9 - a key for the mapv9 - the value for k9public static <K,V> HashMap<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)
K - The key typeV - The value typek1 - a key for the mapv1 - the value for k1k2 - a key for the mapv2 - the value for k2k3 - a key for the mapv3 - the value for k3k4 - a key for the mapv4 - the value for k4k5 - a key for the mapv5 - the value for k5k6 - a key for the mapv6 - the value for k6k7 - a key for the mapv7 - the value for k7k8 - a key for the mapv8 - the value for k8k9 - a key for the mapv9 - the value for k9k10 - a key for the mapv10 - the value for k10public static <K,V> HashMap<K,V> tabulate(int n, java.util.function.Function<? super java.lang.Integer,? extends Tuple2<? extends K,? extends V>> f)
n values of a given Function f
over a range of integer values from 0 to n - 1.K - The key typeV - The value typen - The number of elements in the HashMapf - The Function computing element valuesf(0),f(1), ..., f(n - 1)java.lang.NullPointerException - if f is nullpublic static <K,V> HashMap<K,V> fill(int n, java.util.function.Supplier<? extends Tuple2<? extends K,? extends V>> s)
n calls to a given Supplier s.K - The key typeV - The value typen - The number of elements in the HashMaps - The Supplier computing element valuesn, where each element contains the result supplied by s.java.lang.NullPointerException - if s is null@SafeVarargs public static <K,V> HashMap<K,V> ofEntries(java.util.Map.Entry<? extends K,? extends V>... entries)
K - The key typeV - The value typeentries - Map entries@SafeVarargs public static <K,V> HashMap<K,V> ofEntries(Tuple2<? extends K,? extends V>... entries)
K - The key typeV - The value typeentries - Map entriespublic static <K,V> HashMap<K,V> ofEntries(java.lang.Iterable<? extends Tuple2<? extends K,? extends V>> entries)
K - The key typeV - The value typeentries - Map entriespublic <K2,V2> HashMap<K2,V2> bimap(java.util.function.Function<? super K,? extends K2> keyMapper, java.util.function.Function<? super V,? extends V2> valueMapper)
MapMap to a new Map with different component type by applying a function to its elements.bimap in interface Map<K,V>K2 - key's component type of the map resultV2 - value's component type of the map resultkeyMapper - a Function that maps the keys of type K to keys of type K2valueMapper - a Function that the values of type V to values of type V2Mappublic Tuple2<V,HashMap<K,V>> computeIfAbsent(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
MapcomputeIfAbsent in interface Map<K,V>key - key whose presence in this map is to be testedmappingFunction - mapping functionTuple2 of current or modified map and existing or computed value associated with the specified keypublic Tuple2<Option<V>,HashMap<K,V>> computeIfPresent(K key, java.util.function.BiFunction<? super K,? super V,? extends V> remappingFunction)
MapcomputeIfPresent in interface Map<K,V>key - key whose presence in this map is to be testedremappingFunction - remapping functionTuple2 of current or modified map and the Some of the value associated
with the specified key, or None if nonepublic boolean containsKey(K key)
Maptrue if this map contains a mapping for the specified key.containsKey in interface Map<K,V>key - key whose presence in this map is to be testedtrue if this map contains a mapping for the specified keypublic HashMap<K,V> distinct()
Traversableequals.public HashMap<K,V> distinctBy(java.util.Comparator<? super Tuple2<K,V>> comparator)
Traversablecomparator.distinctBy in interface Map<K,V>distinctBy in interface Traversable<Tuple2<K,V>>comparator - A comparatorTraversable containing this elements without duplicatespublic <U> HashMap<K,V> distinctBy(java.util.function.Function<? super Tuple2<K,V>,? extends U> keyExtractor)
Traversableequals.
The elements of the result are determined in the order of their occurrence - first match wins.
distinctBy in interface Map<K,V>distinctBy in interface Traversable<Tuple2<K,V>>U - key typekeyExtractor - A key extractorTraversable containing this elements without duplicatespublic HashMap<K,V> drop(int n)
Traversablepublic HashMap<K,V> dropRight(int n)
TraversabledropRight in interface Map<K,V>dropRight in interface Traversable<Tuple2<K,V>>n - The number of elements to drop.public HashMap<K,V> dropUntil(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
TraversabledropUntil in interface Map<K,V>dropUntil in interface Traversable<Tuple2<K,V>>predicate - A condition tested subsequently for this elements.public HashMap<K,V> dropWhile(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Traversable
Note: This is essentially the same as dropUntil(predicate.negate()).
It is intended to be used with method references, which cannot be negated directly.
dropWhile in interface Map<K,V>dropWhile in interface Traversable<Tuple2<K,V>>predicate - A condition tested subsequently for this elements.public HashMap<K,V> filter(java.util.function.BiPredicate<? super K,? super V> predicate)
Mappublic HashMap<K,V> filterNot(java.util.function.BiPredicate<? super K,? super V> predicate)
Mappublic HashMap<K,V> filter(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Traversablepublic HashMap<K,V> filterNot(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
TraversableThe default implementation is equivalent to
filter(predicate.negate()public HashMap<K,V> filterKeys(java.util.function.Predicate<? super K> predicate)
MapfilterKeys in interface Map<K,V>predicate - the predicate used to test keys of elementspublic HashMap<K,V> filterNotKeys(java.util.function.Predicate<? super K> predicate)
MapfilterNotKeys in interface Map<K,V>predicate - the predicate used to test keys of elementspublic HashMap<K,V> filterValues(java.util.function.Predicate<? super V> predicate)
MapfilterValues in interface Map<K,V>predicate - the predicate used to test values of elementspublic HashMap<K,V> filterNotValues(java.util.function.Predicate<? super V> predicate)
MapfilterNotValues in interface Map<K,V>predicate - the predicate used to test values of elementspublic <K2,V2> HashMap<K2,V2> flatMap(java.util.function.BiFunction<? super K,? super V,? extends java.lang.Iterable<Tuple2<K2,V2>>> mapper)
MapMap to a new Map with different component type.public Option<V> get(K key)
MapSome of value to which the specified key
is mapped, or None if this map contains no mapping for the key.public V getOrElse(K key, V defaultValue)
Mappublic <C> Map<C,HashMap<K,V>> groupBy(java.util.function.Function<? super Tuple2<K,V>,? extends C> classifier)
TraversablegroupBy in interface Map<K,V>groupBy in interface Traversable<Tuple2<K,V>>C - classified class typeclassifier - A function which classifies elements into classesTraversable.arrangeBy(Function)public Iterator<HashMap<K,V>> grouped(int size)
TraversableTraversable into fixed size blocks.
Let length be the length of this Iterable. Then grouped is defined as follows:
this.isEmpty(), the resulting Iterator is empty.size <= length, the resulting Iterator will contain length / size blocks of size
size and maybe a non-empty block of size length % size, if there are remaining elements.size > length, the resulting Iterator will contain one block of size length.
[].grouped(1) = []
[].grouped(0) throws
[].grouped(-1) throws
[1,2,3,4].grouped(2) = [[1,2],[3,4]]
[1,2,3,4,5].grouped(2) = [[1,2],[3,4],[5]]
[1,2,3,4].grouped(5) = [[1,2,3,4]]
Please note that grouped(int) is a special case of Traversable.sliding(int, int), i.e.
grouped(size) is the same as sliding(size, size).public Tuple2<K,V> head()
Traversablehead in interface Traversable<Tuple2<K,V>>public HashMap<K,V> init()
HashSet.init()public Option<HashMap<K,V>> initOption()
TraversableOption.initOption in interface Map<K,V>initOption in interface Traversable<Tuple2<K,V>>Some(traversable) or None if this is empty.public boolean isAsync()
HashMap is computed synchronously.public boolean isEmpty()
Traversablepublic boolean isLazy()
HashMap is computed eagerly.public Iterator<Tuple2<K,V>> iterator()
Traversablepublic Set<K> keySet()
Mappublic Iterator<K> keysIterator()
MapkeysIterator in interface Map<K,V>Iterator of the keys contained in this map.public Tuple2<K,V> last()
Traversablelast in interface Traversable<Tuple2<K,V>>public <K2,V2> HashMap<K2,V2> map(java.util.function.BiFunction<? super K,? super V,Tuple2<K2,V2>> mapper)
MapMap to form a new Map.public <K2> HashMap<K2,V> mapKeys(java.util.function.Function<? super K,? extends K2> keyMapper)
MapMap while preserving the corresponding values.
The size of the result map may be smaller if keyMapper maps two or more distinct keys to the same new key.
In this case the value at the latest of the original keys is retained.
Order of keys is predictable in TreeMap (by comparator) and LinkedHashMap (insertion-order) and not predictable in HashMap.
public <K2> HashMap<K2,V> mapKeys(java.util.function.Function<? super K,? extends K2> keyMapper, java.util.function.BiFunction<? super V,? super V,? extends V> valueMerge)
MapMap while preserving the corresponding values and applying a value merge function on collisions.
The size of the result map may be smaller if keyMapper maps two or more distinct keys to the same new key.
In this case the associated values will be combined using valueMerge.
public <V2> HashMap<K,V2> mapValues(java.util.function.Function<? super V,? extends V2> valueMapper)
MapMap while preserving the corresponding keys.public HashMap<K,V> merge(Map<? extends K,? extends V> that)
Mapthis map and that map.
If collisions occur, the value of this map is taken.
public <U extends V> HashMap<K,V> merge(Map<? extends K,U> that, java.util.function.BiFunction<? super V,? super U,? extends V> collisionResolution)
Mapthis map and that map.
Uses the specified collision resolution function if two keys are the same.
The collision resolution function will always take the first argument from this map
and the second from that map.
public HashMap<K,V> orElse(java.lang.Iterable<? extends Tuple2<K,V>> other)
TraversableTraversable if it is nonempty, otherwise return the alternative.public HashMap<K,V> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends Tuple2<K,V>>> supplier)
TraversableTraversable if it is nonempty, otherwise return the result of evaluating supplier.public Tuple2<HashMap<K,V>,HashMap<K,V>> partition(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
TraversableTraversable by splitting this elements in two in distinct traversables
according to a predicate.partition in interface Map<K,V>partition in interface Traversable<Tuple2<K,V>>predicate - A predicate which classifies an element if it is in the first or the second traversable.Traversable contains all elements that satisfy the given predicate, the second Traversable contains all elements that don't. The original order of elements is preserved.public HashMap<K,V> peek(java.util.function.Consumer<? super Tuple2<K,V>> action)
Valueaction on the first element if this is an eager implementation.
Performs the given action on all elements (the first immediately, successive deferred),
if this is a lazy implementation.public <U extends V> HashMap<K,V> put(K key, U value, java.util.function.BiFunction<? super V,? super U,? extends V> merge)
Mapput in interface Map<K,V>U - the value typekey - key with which the specified value is to be associatedvalue - value to be associated with the specified keymerge - function taking the old and new values and merging them.public HashMap<K,V> put(K key, V value)
Mappublic HashMap<K,V> put(Tuple2<? extends K,? extends V> entry)
Mapput(entry._1, entry._2).public <U extends V> HashMap<K,V> put(Tuple2<? extends K,U> entry, java.util.function.BiFunction<? super V,? super U,? extends V> merge)
Mapput(entry._1, entry._2, merge).public HashMap<K,V> remove(K key)
Mappublic HashMap<K,V> removeAll(java.lang.Iterable<? extends K> c)
Mappublic HashMap<K,V> replace(Tuple2<K,V> currentElement, Tuple2<K,V> newElement)
Traversablereplace in interface Map<K,V>replace in interface Traversable<Tuple2<K,V>>currentElement - An element to be substituted.newElement - A replacement for currentElement.public HashMap<K,V> replaceAll(Tuple2<K,V> currentElement, Tuple2<K,V> newElement)
TraversablereplaceAll in interface Map<K,V>replaceAll in interface Traversable<Tuple2<K,V>>currentElement - An element to be substituted.newElement - A replacement for currentElement.public HashMap<K,V> replaceValue(K key, V value)
MapreplaceValue in interface Map<K,V>key - the key of the element to be substituted.value - the new value to be associated with the keypublic HashMap<K,V> replace(K key, V oldValue, V newValue)
Mapreplace in interface Map<K,V>key - the key of the element to be substituted.oldValue - the expected current value that the key is currently mapped tonewValue - the new value to be associated with the keypublic HashMap<K,V> replaceAll(java.util.function.BiFunction<? super K,? super V,? extends V> function)
MapreplaceAll in interface Map<K,V>function - function transforming key and current value to a new valuepublic HashMap<K,V> retainAll(java.lang.Iterable<? extends Tuple2<K,V>> elements)
Traversablepublic HashMap<K,V> scan(Tuple2<K,V> zero, java.util.function.BiFunction<? super Tuple2<K,V>,? super Tuple2<K,V>,? extends Tuple2<K,V>> operation)
Traversablescan in interface Map<K,V>scan in interface Traversable<Tuple2<K,V>>zero - neutral element for the operator opoperation - the associative operator for the scanpublic int size()
Traversable
Same as Traversable.length().
public Iterator<HashMap<K,V>> slideBy(java.util.function.Function<? super Tuple2<K,V>,?> classifier)
TraversableTraversable.
Each window contains elements with the same class, as determined by classifier. Two consecutive
values in this Traversable will be in the same window only if classifier returns equal
values for them. Otherwise, the values will constitute the last element of the previous window and the
first element of the next window.
Examples:
[].slideBy(Function.identity()) = []
[1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]]
[1,2,3,10,12,5,7,20,29].slideBy(x -> x/10) = [[1,2,3],[10,12],[5,7],[20,29]]
public Iterator<HashMap<K,V>> sliding(int size)
Traversablesize and step size 1 over this Traversable by calling
Traversable.sliding(int, int).public Iterator<HashMap<K,V>> sliding(int size, int step)
Traversablesize and step size over this Traversable.
Examples:
[].sliding(1,1) = []
[1,2,3,4,5].sliding(2,3) = [[1,2],[4,5]]
[1,2,3,4,5].sliding(2,4) = [[1,2],[5]]
[1,2,3,4,5].sliding(2,5) = [[1,2]]
[1,2,3,4].sliding(5,3) = [[1,2,3,4]]
public Tuple2<HashMap<K,V>,HashMap<K,V>> span(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Traversablepredicate and the second element is the remainder.public java.util.Spliterator<Tuple2<K,V>> spliterator()
spliterator in interface Traversable<Tuple2<K,V>>spliterator in interface Value<Tuple2<K,V>>spliterator in interface java.lang.Iterable<Tuple2<K,V>>public HashMap<K,V> tail()
Traversablepublic Option<HashMap<K,V>> tailOption()
TraversableOption.tailOption in interface Map<K,V>tailOption in interface Traversable<Tuple2<K,V>>Some(traversable) or None if this is empty.public HashMap<K,V> take(int n)
Traversable
The result is equivalent to sublist(0, max(0, min(length(), n))) but does not throw if n < 0 or
n > length().
In the case of n < 0 the empty instance is returned, in the case of n > length() this is returned.
public HashMap<K,V> takeRight(int n)
Traversable
The result is equivalent to sublist(max(0, min(length(), length() - n)), n), i.e. takeRight will not
throw if n < 0 or n > length().
In the case of n < 0 the empty instance is returned, in the case of n > length() this is returned.
public HashMap<K,V> takeUntil(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
Traversable
Note: This is essentially the same as takeWhile(predicate.negate()). It is intended to be used with
method references, which cannot be negated directly.
public HashMap<K,V> takeWhile(java.util.function.Predicate<? super Tuple2<K,V>> predicate)
TraversabletakeWhile in interface Map<K,V>takeWhile in interface Traversable<Tuple2<K,V>>predicate - A condition tested subsequently for the contained elements.public java.util.HashMap<K,V> toJavaMap()
MapMap to a java.util.Map while preserving characteristics
like insertion order (LinkedHashMap) and sort order (SortedMap).public Stream<V> values()
MapSeq that contains the values of this Map.
// = Seq("a", "b", "c")
HashMap.of(1, "a", 2, "b", 3, "c").values()
public Iterator<V> valuesIterator()
Map
// = Iterator.of("a", "b", "c")
HashMap.of(1, "a", 2, "b", 3, "c").values()
valuesIterator in interface Map<K,V>Iteratorpublic boolean equals(java.lang.Object other)
TraversableNotes:
equals in interface Traversable<Tuple2<K,V>>equals in interface Value<Tuple2<K,V>>equals in class java.lang.Objectother - an object, may be nullpublic int hashCode()
Traversable
int hash = 1;
for (T t : this) { hash = hash * 31 + Objects.hashCode(t); }
Collections with arbitrary iteration order are hashed in a way such that the hash of a fixed number of elements is independent of their iteration order.
int hash = 1;
for (T t : this) { hash += Objects.hashCode(t); }
Please note that the particular hashing algorithms may change in a future version of Vavr.
public final class Hashed<K> {
private final K key;
private final Lazy<Integer> hashCode;
public Hashed(K key) {
this.key = key;
this.hashCode = Lazy.of(() -> Objects.hashCode(key));
}
public K key() {
return key;
}
@Override
public boolean equals(Object o) {
if (o == key) {
return true;
} else if (key != null && o instanceof Hashed) {
final Hashed that = (Hashed) o;
return key.equals(that.key);
} else {
return false;
}
}
@Override
public int hashCode() {
return hashCode.get();
}
@Override
public String toString() {
return "Hashed(" + (key == null ? "null" : key.toString()) + ")";
}
}
public java.lang.String stringPrefix()
ValuestringPrefix in interface Value<Tuple2<K,V>>public java.lang.String toString()
ValueSee Object.toString().