Class AbstractMultimap<K, V, M extends Multimap<K,V>>
- Type Parameters:
K- Key typeV- Value typeM- Concrete Multimap type extending this class
- All Implemented Interfaces:
Foldable<Tuple2<K,V>>, Multimap<K, V>, Traversable<Tuple2<K, V>>, Function1<K, Traversable<V>>, PartialFunction<K, Traversable<V>>, Value<Tuple2<K, V>>, Serializable, Iterable<Tuple2<K, V>>, Function<K, Traversable<V>>
- Direct Known Subclasses:
HashMultimap, LinkedHashMultimap, TreeMultimap
Multimap interface that provides
common functionality for concrete Multimap implementations. This class is not
intended to be public and serves as a foundation for specialized Multimap types.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static interfaceNested classes/interfaces inherited from interface Multimap
Multimap.ContainerType -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<K, Traversable<V>> The backing map that stores the key-value pairs.private final Multimap.ContainerTypeprotected final AbstractMultimap.SerializableSupplier<Traversable<?>> A supplier that creates empty containers for new key entries.private static final long -
Constructor Summary
ConstructorsConstructorDescriptionAbstractMultimap(Map<K, Traversable<V>> back, Multimap.ContainerType containerType, AbstractMultimap.SerializableSupplier<Traversable<?>> emptyContainer) Creates a new AbstractMultimap with the specified backing map, container type, and empty container supplier. -
Method Summary
Modifier and TypeMethodDescriptionMap<K, Traversable<V>> asMap()Converts thisMultimapto aMap<K2,V2> Multimap <K2, V2> bimap(@NonNull Function<? super K, ? extends K2> keyMapper, @NonNull Function<? super V, ? extends V2> valueMapper) Maps thisMultimapto a newMultimapwith different component type by applying a function to its elements.booleancontainsKey(K key) Returnstrueif this multimap contains a mapping for the specified key.private <K2,V2> Multimap <K2, V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) Creates a new Multimap from the given entries by grouping values by their keys.protected abstract <K2,V2> Multimap <K2, V2> createFromMap(Map<K2, Traversable<V2>> back) Creates a new Multimap instance from the given backing map.distinct()Returns a newTraversablecontaining the elements of this instance with all duplicates removed.distinctBy(@NonNull Comparator<? super Tuple2<K, V>> comparator) Returns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.<U> MdistinctBy(@NonNull Function<? super Tuple2<K, V>, ? extends U> keyExtractor) Returns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.drop(int n) Returns a newTraversablewithout the firstnelements, or an empty instance if this contains fewer thannelements.dropRight(int n) Returns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements.Returns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements.Returns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.protected abstract <K2,V2> Multimap <K2, V2> Returns an empty Multimap instance specific to the implementing class.protected abstract <K2,V2> Map <K2, V2> Returns an empty Map instance specific to the implementing class.booleanDetermines whether this collection is equal to the given object.filter(@NonNull BiPredicate<? super K, ? super V> predicate) Returns a new Multimap consisting of all elements which satisfy the given predicate.Returns a new traversable containing only the elements that satisfy the given predicate.filterKeys(@NonNull Predicate<? super K> predicate) Returns a new Multimap consisting of all elements with keys which satisfy the given predicate.filterValues(@NonNull Predicate<? super V> predicate) Returns a new Multimap consisting of all elements with values which satisfy the given predicate.<K2,V2> Multimap <K2, V2> FlatMaps thisMultimapto a newMultimapwith different component type.Returns theSomeof value to which the specified key is mapped, orNoneif this multimap contains no mapping for the key.Returns the type of theTraversablevalue container of thisMultiMap.getOrElse(K key, Traversable<? extends V> defaultValue) Returns the value associated with a key, or a default value if the key is not contained in the mapGroups elements of thisTraversablebased on a classifier function.grouped(int size) Splits thisTraversableinto consecutive blocks of the given size.inthashCode()Returns the hash code of this collection.head()Returns the first element of this non-emptyTraversable.init()Returns all elements of this Traversable except the last one.Returns all elements of this Traversable except the last one, wrapped in anOption.booleanisAsync()Checks if thisValueis asynchronously (short: async) computed.booleanisEmpty()Checks if this Traversable contains no elements.booleanisLazy()Checks if thisValueis lazily evaluated.iterator()Returns an iterator over the elements of this Traversable, implemented viaTraversable.head()andTraversable.tail().keySet()Returns the keys contained in this multimap.last()Returns the last element of this Traversable.<K2,V2> Multimap <K2, V2> map(@NonNull BiFunction<? super K, ? super V, Tuple2<K2, V2>> mapper) Maps the entries of thisMultimapto form a newMultimap.Maps the values of thisMultimapwhile preserving the corresponding keys.Creates a new multimap which by merging the entries ofthismultimap andthatmultimap.merge(@NonNull Multimap<K2, V2> that, @NonNull BiFunction<Traversable<V>, Traversable<V2>, Traversable<V>> collisionResolution) Creates a new multimap which by merging the entries ofthismultimap andthatmultimap.Returns thisMultimapif it is nonempty, otherwiseMultimapcreated from iterable, using existing multimap properties.Returns thisMultimapif it is nonempty, otherwiseMultimapcreated from result of evaluating supplier, using existing multimap properties.Splits thisTraversableinto two partitions according to a predicate.Performs the givenactionon the first element if this is an eager implementation.Convenience method forput(entry._1, entry._2).Associates the specified value with the specified key in this multimap.reject(@NonNull BiPredicate<? super K, ? super V> predicate) Returns a new Multimap consisting of all elements which do not satisfy the given predicate.Returns a new traversable containing only the elements that do not satisfy the given predicate.rejectKeys(@NonNull Predicate<? super K> predicate) Returns a new Multimap consisting of all elements with keys which do not satisfy the given predicate.rejectValues(@NonNull Predicate<? super V> predicate) Returns a new Multimap consisting of all elements with values which do not satisfy the given predicate.Removes the mapping for a key from this multimap if it is present.Removes the key-value pair from this multimap if it is present.Removes the mapping for a key from this multimap if it is present.removeAll(@NonNull BiPredicate<? super K, ? super V> predicate) Deprecated.removeKeys(@NonNull Predicate<? super K> predicate) Deprecated.removeValues(@NonNull Predicate<? super V> predicate) Deprecated.Replaces the first occurrence ofcurrentElementwithnewElement, if it exists.Replaces the entry with the specified key and oldValue.Replaces all occurrences ofcurrentElementwithnewElement.replaceAll(@NonNull BiFunction<? super K, ? super V, ? extends V> function) Replaces each entry's values with the result of invoking the given function on that each tuple until all entries have been processed or the function throws an exception.replaceValue(K key, V value) Replaces the entry for the specified key only if it is currently mapped to some value.Retains only the elements from this Traversable that are contained in the givenelements.scan(Tuple2<K, V> zero, @NonNull BiFunction<? super Tuple2<K, V>, ? super Tuple2<K, V>, ? extends Tuple2<K, V>> operation) Computes a prefix scan of the elements of this Traversable.intsize()Returns the number of elements in this Traversable.Partitions thisTraversableinto consecutive non-overlapping windows according to a classification function.sliding(int size) Slides a window of a givensizeover thisTraversablewith a step size of 1.sliding(int size, int step) Slides a window of a specificsizewith a givenstepover thisTraversable.Splits thisTraversableinto a prefix and remainder according to the givenpredicate.Returns the name of this Value type, which is used by toString().tail()Returns a newTraversablewithout its first element.Returns a newTraversablewithout its first element as anOption.take(int n) Returns the firstnelements of thisTraversable, or all elements ifnexceeds the length.takeRight(int n) Returns the lastnelements of thisTraversable, or all elements ifnexceeds the length.Takes elements from thisTraversableuntil the given predicate holds for an element.Takes elements from thisTraversablewhile the given predicate holds.Map<K, Collection<V>> Converts this VavrMapto ajava.util.Mapwhile preserving characteristics like insertion order (LinkedHashMultimap) and sort order (SortedMultimap).protected <JM extends Map<K, Collection<V>>>
JMtoJavaMap(JM javaMap) Converts thisMultimapto a JavaMapusing the provided map instance.toString()Clarifies that values have a proper toString() method implemented.values()Returns aTraversablecontaining all the values of thisMultimap.Methods inherited from interface Foldable
fold, reduce, reduceOptionMethods inherited from interface Function1
andThen, arity, compose, compose1, curried, isMemoized, memoized, partial, reversed, tupledMethods inherited from interface Multimap
apply, asPartialFunction, collect, contains, containsValue, flatMap, foldRight, forEach, hasDefiniteSize, isDefinedAt, isDistinct, isTraversableAgain, iterator, length, map, mapTo, mapToVoid, scanLeft, scanRight, transform, unzip, unzip, unzip3, unzip3, zip, zipAll, zipWith, zipWithIndex, zipWithIndexMethods inherited from interface PartialFunction
liftMethods inherited from interface Traversable
arrangeBy, average, containsAll, count, existsUnique, find, findLast, foldLeft, forEachWithIndex, get, headOption, isOrdered, isSequential, isSingleValued, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, nonEmpty, product, reduceLeft, reduceLeftOption, reduceRight, reduceRightOption, single, singleOption, spliterator, sumMethods inherited from interface Value
collect, collect, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, 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, toVector
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
back
The backing map that stores the key-value pairs. -
emptyContainer
A supplier that creates empty containers for new key entries. -
containerType
-
-
Constructor Details
-
AbstractMultimap
AbstractMultimap(Map<K, Traversable<V>> back, Multimap.ContainerType containerType, AbstractMultimap.SerializableSupplier<Traversable<?>> emptyContainer) Creates a new AbstractMultimap with the specified backing map, container type, and empty container supplier.- Parameters:
back- The backing map that stores the key-value pairscontainerType- The type of container used to store multiple values for a keyemptyContainer- A supplier that creates empty containers for new key entries
-
-
Method Details
-
emptyMapSupplier
Returns an empty Map instance specific to the implementing class.- Type Parameters:
K2- Key type of the empty mapV2- Value type of the empty map- Returns:
- An empty Map instance
-
emptyInstance
Returns an empty Multimap instance specific to the implementing class.- Type Parameters:
K2- Key type of the empty multimapV2- Value type of the empty multimap- Returns:
- An empty Multimap instance
-
createFromMap
Creates a new Multimap instance from the given backing map.- Type Parameters:
K2- Key type of the new multimapV2- Value type of the new multimap- Parameters:
back- The backing map to create the multimap from- Returns:
- A new Multimap instance containing the entries from the backing map
-
createFromEntries
private <K2,V2> Multimap<K2,V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) Creates a new Multimap from the given entries by grouping values by their keys. For each key, a new container is created using the emptyContainer supplier, and values are added using the containerType's add operation.- Type Parameters:
K2- Key type of the new multimapV2- Value type of the new multimap- Parameters:
entries- The entries to create the multimap from- Returns:
- A new Multimap containing all the entries with values grouped by keys
-
asMap
-
bimap
public <K2,V2> Multimap<K2,V2> bimap(@NonNull Function<? super K, ? extends K2> keyMapper, @NonNull Function<? super V, ? extends V2> valueMapper) Description copied from interface:MultimapMaps thisMultimapto a newMultimapwith different component type by applying a function to its elements.- Specified by:
bimapin interfaceMultimap<K,V> - Type Parameters:
K2- key's component type of the multimap resultV2- value's component type of the multimap result- Parameters:
keyMapper- aFunctionthat maps the keys of typeKto keys of typeK2valueMapper- aFunctionthat the values of typeVto values of typeV2- Returns:
- a new
Multimap
-
containsKey
Description copied from interface:MultimapReturnstrueif this multimap contains a mapping for the specified key.- Specified by:
containsKeyin interfaceMultimap<K,V> - Parameters:
key- key whose presence in this multimap is to be tested- Returns:
trueif this multimap contains a mapping for the specified key
-
getContainerType
Description copied from interface:MultimapReturns the type of theTraversablevalue container of thisMultiMap.- Specified by:
getContainerTypein interfaceMultimap<K,V> - Returns:
- an enum value representing the container type
-
flatMap
-
get
Description copied from interface:MultimapReturns theSomeof value to which the specified key is mapped, orNoneif this multimap contains no mapping for the key. -
getOrElse
Description copied from interface:MultimapReturns the value associated with a key, or a default value if the key is not contained in the map -
keySet
-
map
Description copied from interface:MultimapMaps the entries of thisMultimapto form a newMultimap. -
mapValues
Description copied from interface:MultimapMaps the values of thisMultimapwhile preserving the corresponding keys. -
put
Description copied from interface:MultimapAssociates the specified value with the specified key in this multimap. If the map previously contained a mapping for the key, the old value is replaced by the specified value. -
put
Description copied from interface:MultimapConvenience method forput(entry._1, entry._2). -
remove
Description copied from interface:MultimapRemoves the mapping for a key from this multimap if it is present. -
remove
Description copied from interface:MultimapRemoves the key-value pair from this multimap if it is present. -
removeAll
Description copied from interface:MultimapRemoves the mapping for a key from this multimap if it is present. -
size
public int size()Description copied from interface:TraversableReturns the number of elements in this Traversable.Alias for
Traversable.length(). -
values
Description copied from interface:MultimapReturns aTraversablecontaining all the values of thisMultimap.- Specified by:
valuesin interfaceMultimap<K,V> - Returns:
- a new
Traversable
-
distinct
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance with all duplicates removed. Element equality is determined usingequals. -
distinctBy
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, as determined by the givencomparator.- Specified by:
distinctByin interfaceMultimap<K,V> - Specified by:
distinctByin interfaceTraversable<K>- Parameters:
comparator- a comparator used to determine equality of elements- Returns:
- a new
Traversablewith duplicates removed
-
distinctBy
Description copied from interface:TraversableReturns a newTraversablecontaining the elements of this instance without duplicates, based on keys extracted from elements usingkeyExtractor.The first occurrence of each key is retained in the resulting sequence.
- Specified by:
distinctByin interfaceMultimap<K,V> - Specified by:
distinctByin interfaceTraversable<K>- Type Parameters:
U- the type of key- Parameters:
keyExtractor- a function to extract keys for determining uniqueness- Returns:
- a new
Traversablewith duplicates removed based on keys
-
drop
Description copied from interface:TraversableReturns a newTraversablewithout the firstnelements, or an empty instance if this contains fewer thannelements. -
dropRight
Description copied from interface:TraversableReturns a newTraversablewithout the lastnelements, or an empty instance if this contains fewer thannelements. -
dropUntil
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that satisfies the givenpredicate, dropping all preceding elements. -
dropWhile
Description copied from interface:TraversableReturns a newTraversablestarting from the first element that does not satisfy the givenpredicate, dropping all preceding elements.This is equivalent to
dropUntil(predicate.negate()), which is useful for method references that cannot be negated directly. -
filter
Description copied from interface:TraversableReturns a new traversable containing only the elements that satisfy the given predicate. -
reject
Description copied from interface:TraversableReturns a new traversable containing only the elements that do not satisfy the given predicate.This is equivalent to
filter(predicate.negate()). -
filter
Description copied from interface:MultimapReturns a new Multimap consisting of all elements which satisfy the given predicate. -
reject
Description copied from interface:MultimapReturns a new Multimap consisting of all elements which do not satisfy the given predicate. -
filterKeys
Description copied from interface:MultimapReturns a new Multimap consisting of all elements with keys which satisfy the given predicate.- Specified by:
filterKeysin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test keys of elements- Returns:
- a new Multimap
-
rejectKeys
Description copied from interface:MultimapReturns a new Multimap consisting of all elements with keys which do not satisfy the given predicate.- Specified by:
rejectKeysin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test keys of elements- Returns:
- a new Multimap
-
filterValues
Description copied from interface:MultimapReturns a new Multimap consisting of all elements with values which satisfy the given predicate.- Specified by:
filterValuesin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test values of elements- Returns:
- a new Multimap
-
rejectValues
Description copied from interface:MultimapReturns a new Multimap consisting of all elements with values which do not satisfy the given predicate.- Specified by:
rejectValuesin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test values of elements- Returns:
- a new Multimap
-
removeAll
Deprecated.Description copied from interface:MultimapReturns a new Multimap consisting of all elements which do not satisfy the given predicate. -
removeKeys
Deprecated.Description copied from interface:MultimapReturns a new Multimap consisting of all elements with keys which do not satisfy the given predicate.- Specified by:
removeKeysin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test keys of elements- Returns:
- a new Multimap
-
removeValues
Deprecated.Description copied from interface:MultimapReturns a new Multimap consisting of all elements with values which do not satisfy the given predicate.- Specified by:
removeValuesin interfaceMultimap<K,V> - Parameters:
predicate- the predicate used to test values of elements- Returns:
- a new Multimap
-
groupBy
Description copied from interface:TraversableGroups elements of thisTraversablebased on a classifier function. -
grouped
Description copied from interface:TraversableSplits thisTraversableinto consecutive blocks of the given size.Let
lengthbe the number of elements in thisTraversable:- If empty, the resulting
Iteratoris empty. - If
size <= length, the resultingIteratorcontainslength / sizeblocks of sizesizeand possibly a final smaller block of sizelength % size. - If
size > length, the resultingIteratorcontains a single block of sizelength.
Examples:
[].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]]Note:
grouped(size)is equivalent tosliding(size, size). - If empty, the resulting
-
init
Description copied from interface:TraversableReturns all elements of this Traversable except the last one.This is the dual of
Traversable.tail(). -
head
Description copied from interface:TraversableReturns the first element of this non-emptyTraversable.- Specified by:
headin interfaceTraversable<K>- Returns:
- the first element
-
initOption
Description copied from interface:TraversableReturns all elements of this Traversable except the last one, wrapped in anOption.This is the dual of
Traversable.tailOption().- Specified by:
initOptionin interfaceMultimap<K,V> - Specified by:
initOptionin interfaceTraversable<K>- Returns:
Some(traversable)if non-empty, orNoneif this Traversable is empty
-
isAsync
public boolean isAsync()Description copied from interface:ValueChecks if thisValueis asynchronously (short: async) computed.Methods of a
Valueinstance that operate on the underlying value may block the current thread until the value is present and the computation can be performed. -
isEmpty
public boolean isEmpty()Description copied from interface:TraversableChecks if this Traversable contains no elements. -
isLazy
-
iterator
Description copied from interface:TraversableReturns an iterator over the elements of this Traversable, implemented viaTraversable.head()andTraversable.tail(). Subclasses may override for a more efficient implementation. -
last
Description copied from interface:TraversableReturns the last element of this Traversable.- Specified by:
lastin interfaceTraversable<K>- Returns:
- the last element
-
merge
Description copied from interface:MultimapCreates a new multimap which by merging the entries ofthismultimap andthatmultimap.If collisions occur, the value of
thismultimap is taken. -
merge
public <K2 extends K, V2 extends V> M merge(@NonNull Multimap<K2, V2> that, @NonNull BiFunction<Traversable<V>, Traversable<V2>, Traversable<V>> collisionResolution) Description copied from interface:MultimapCreates a new multimap which by merging the entries ofthismultimap andthatmultimap.Uses the specified collision resolution function if two keys are the same. The collision resolution function will always take the first argument from
thismultimap and the second fromthatmultimap. -
orElse
Returns thisMultimapif it is nonempty, otherwiseMultimapcreated from iterable, using existing multimap properties. -
orElse
Returns thisMultimapif it is nonempty, otherwiseMultimapcreated from result of evaluating supplier, using existing multimap properties. -
partition
Description copied from interface:TraversableSplits thisTraversableinto two partitions according to a predicate.The first partition contains all elements that satisfy the predicate, and the second contains all elements that do not. The original iteration order is preserved.
-
peek
Description copied from interface:ValuePerforms the givenactionon the first element if this is an eager implementation. Performs the givenactionon all elements (the first immediately, successive deferred), if this is a lazy implementation. -
replace
Description copied from interface:TraversableReplaces the first occurrence ofcurrentElementwithnewElement, if it exists. -
replaceAll
Description copied from interface:TraversableReplaces all occurrences ofcurrentElementwithnewElement.- Specified by:
replaceAllin interfaceMultimap<K,V> - Specified by:
replaceAllin interfaceTraversable<K>- Parameters:
currentElement- the element to be replacednewElement- the replacement element- Returns:
- a new Traversable with all occurrences of
currentElementreplaced bynewElement
-
replaceValue
Description copied from interface:MultimapReplaces the entry for the specified key only if it is currently mapped to some value.- Specified by:
replaceValuein interfaceMultimap<K,V> - Parameters:
key- the key of the element to be substitutedvalue- the new value to be associated with the key- Returns:
- a new map containing key mapped to value if key was contained before. The old map otherwise
-
replace
Description copied from interface:MultimapReplaces the entry with the specified key and oldValue.- Specified by:
replacein interfaceMultimap<K,V> - Parameters:
key- the key of the element to be substitutedoldValue- the expected current value associated with the keynewValue- the new value to be associated with the key- Returns:
- a new map containing key mapped to newValue if key was contained before and oldValue was associated with the key. The old map otherwise.
-
replaceAll
Description copied from interface:MultimapReplaces each entry's values with the result of invoking the given function on that each tuple until all entries have been processed or the function throws an exception.- Specified by:
replaceAllin interfaceMultimap<K,V> - Parameters:
function- function transforming key and current value to a new value- Returns:
- a new map with the same keySet but transformed values
-
retainAll
Description copied from interface:TraversableRetains only the elements from this Traversable that are contained in the givenelements. -
scan
-
slideBy
Description copied from interface:TraversablePartitions thisTraversableinto consecutive non-overlapping windows according to a classification function.Each window contains elements with the same class, as determined by
classifier. Two consecutive elements belong to the same window only ifclassifierreturns equal values for both. Otherwise, the current window ends and a new window begins with the next element.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]] -
sliding
Description copied from interface:TraversableSlides a window of a givensizeover thisTraversablewith a step size of 1.This is equivalent to calling
Traversable.sliding(int, int)with a step size of 1. -
sliding
Description copied from interface:TraversableSlides a window of a specificsizewith a givenstepover thisTraversable.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],[4]] -
span
Description copied from interface:TraversableSplits thisTraversableinto a prefix and remainder according to the givenpredicate.The first element of the returned
Tupleis the longest prefix of elements satisfyingpredicate, and the second element is the remaining elements. -
tail
Description copied from interface:TraversableReturns a newTraversablewithout its first element. -
tailOption
Description copied from interface:TraversableReturns a newTraversablewithout its first element as anOption.- Specified by:
tailOptionin interfaceMultimap<K,V> - Specified by:
tailOptionin interfaceTraversable<K>- Returns:
Some(traversable)if non-empty, otherwiseNone
-
take
Description copied from interface:TraversableReturns the firstnelements of thisTraversable, or all elements ifnexceeds the length.Equivalent to
sublist(0, max(0, min(length(), n))), but safe forn < 0orn > length().If
n < 0, an empty instance is returned. Ifn > length(), the full instance is returned. -
takeRight
Description copied from interface:TraversableReturns the lastnelements of thisTraversable, or all elements ifnexceeds the length.Equivalent to
sublist(max(0, length() - n), length()), but safe forn < 0orn > length().If
n < 0, an empty instance is returned. Ifn > length(), the full instance is returned. -
takeUntil
Description copied from interface:TraversableTakes elements from thisTraversableuntil the given predicate holds for an element.Equivalent to
takeWhile(predicate.negate()), but useful when using method references that cannot be negated directly. -
takeWhile
Description copied from interface:TraversableTakes elements from thisTraversablewhile the given predicate holds.- Specified by:
takeWhilein interfaceMultimap<K,V> - Specified by:
takeWhilein interfaceTraversable<K>- Parameters:
predicate- a condition tested sequentially on the elements- Returns:
- a new
Traversablecontaining all elements up to (but not including) the first one that does not satisfy the predicate
-
equals
Description copied from interface:TraversableDetermines whether this collection is equal to the given object.In Vavr, there are four basic collection types:
Seq– sequential elementsSet– distinct elementsMap– key-value pairsMultimap– keys mapped to multiple values
- They are of the same collection type (Seq, Set, Map, Multimap)
- They contain the same elements
- For
Seq, the element order is the same
For
MapandMultimap, two entries(key1, value1)and(key2, value2)are equal if both their keys and values are equal.Additional notes:
- No collection equals
null(e.g.,Queue(1) != null) - Null elements are allowed and treated as expected
(e.g.,
List(null, 1) == Stream(null, 1),HashMap((null,1)) == LinkedHashMap((null,1))) - Element order matters only for
Seq - Other collection classes are equal if their types and elements (in iteration order) are equal
- Iterators are compared by reference only
-
hashCode
public int hashCode()Description copied from interface:TraversableReturns the hash code of this collection.Vavr distinguishes between collections with predictable iteration order (like
Seq) and collections with arbitrary iteration order (likeSet,Map, andMultimap). In all cases, the hash of an empty collection is defined as1.For collections with predictable iteration order, the hash is computed as:
int hash = 1; for (T t : this) { hash = hash * 31 + Objects.hashCode(t); }For collections with arbitrary iteration order, the hash is computed to be independent of element order:
int hash = 1; for (T t : this) { hash += Objects.hashCode(t); }Note that these algorithms may change in future Vavr versions. Hash codes are generally not cached, unlike size/length, because caching would increase memory usage due to persistent tree-based structures. Computing the hash code is linear in time, O(n). For frequently re-used collections (e.g., as
HashMapkeys), caching can be done externally using a wrapper, for example: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; if (key != null && o instanceof Hashed) return key.equals(((Hashed<?>) o).key); return false; } @Override public int hashCode() { return hashCode.get(); } @Override public String toString() { return "Hashed(" + key + ")"; } } -
stringPrefix
Description copied from interface:ValueReturns the name of this Value type, which is used by toString().- Specified by:
stringPrefixin interfaceValue<K>- Returns:
- This type name.
-
toString
-
toJavaMap
Description copied from interface:MultimapConverts this VavrMapto ajava.util.Mapwhile preserving characteristics like insertion order (LinkedHashMultimap) and sort order (SortedMultimap). -
toJavaMap
Converts thisMultimapto a JavaMapusing the provided map instance.- Type Parameters:
JM- The type of the Java map- Parameters:
javaMap- The Java map instance to populate with this multimap's entries- Returns:
- The populated Java map
-