T
- Component typepublic interface Traversable<T> extends Foldable<T>, Value<T>
Iterable.iterator()
, which may vary each time it is called.
Basic operations:
collect(PartialFunction)
Value.contains(Object)
containsAll(Iterable)
head()
headOption()
init()
initOption()
isEmpty()
last()
lastOption()
length()
size()
tail()
tailOption()
forEachWithIndex(ObjIntConsumer)
grouped(int)
iterator()
slideBy(Function)
sliding(int)
sliding(int, int)
average()
max()
maxBy(Comparator)
maxBy(Function)
min()
minBy(Comparator)
minBy(Function)
product()
sum()
count(Predicate)
Foldable.fold(Object, BiFunction)
foldLeft(Object, BiFunction)
foldRight(Object, BiFunction)
mkString()
mkString(CharSequence)
mkString(CharSequence, CharSequence, CharSequence)
Foldable.reduce(BiFunction)
Foldable.reduceOption(BiFunction)
reduceLeft(BiFunction)
reduceLeftOption(BiFunction)
reduceRight(BiFunction)
reduceRightOption(BiFunction)
drop(int)
dropRight(int)
dropUntil(Predicate)
dropWhile(Predicate)
filter(Predicate)
reject(Predicate)
find(Predicate)
findLast(Predicate)
groupBy(Function)
partition(Predicate)
retainAll(Iterable)
take(int)
takeRight(int)
takeUntil(Predicate)
takeWhile(Predicate)
existsUnique(Predicate)
hasDefiniteSize()
isDistinct()
isOrdered()
isSequential()
isTraversableAgain()
distinct()
distinctBy(Comparator)
distinctBy(Function)
flatMap(Function)
map(Function)
replace(Object, Object)
replaceAll(Object, Object)
scan(Object, BiFunction)
scanLeft(Object, BiFunction)
scanRight(Object, BiFunction)
span(Predicate)
unzip(Function)
unzip3(Function)
zip(Iterable)
zipAll(Iterable, Object, Object)
zipWithIndex()
Modifier and Type | Method and Description |
---|---|
default <K> Option<Map<K,T>> |
arrangeBy(Function<? super T,? extends K> getKey)
Matches each element with a unique key that you extract from it.
|
default Option<Double> |
average()
Calculates the average of this elements, assuming that the element type is
Number . |
<R> Traversable<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects all elements that are in the domain of the given
partialFunction by mapping the elements to type R . |
default boolean |
containsAll(Iterable<? extends T> elements)
Tests if this Traversable contains all given elements.
|
default int |
count(Predicate<? super T> predicate)
Counts the elements which satisfy the given predicate.
|
Traversable<T> |
distinct()
Returns a new version of this which contains no duplicates.
|
Traversable<T> |
distinctBy(Comparator<? super T> comparator)
Returns a new version of this which contains no duplicates.
|
<U> Traversable<T> |
distinctBy(Function<? super T,? extends U> keyExtractor)
Returns a new version of this which contains no duplicates.
|
Traversable<T> |
drop(int n)
Drops the first n elements of this or all elements, if this length < n.
|
Traversable<T> |
dropRight(int n)
Drops the last n elements of this or all elements, if this length < n.
|
Traversable<T> |
dropUntil(Predicate<? super T> predicate)
Drops elements until the predicate holds for the current element.
|
Traversable<T> |
dropWhile(Predicate<? super T> predicate)
Drops elements while the predicate holds for the current element.
|
boolean |
equals(Object obj)
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.
|
default boolean |
existsUnique(Predicate<? super T> predicate)
Checks, if a unique elements exists such that the predicate holds.
|
Traversable<T> |
filter(Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which satisfy the given predicate.
|
default Option<T> |
find(Predicate<? super T> predicate)
Returns the first element of this which satisfies the given predicate.
|
default Option<T> |
findLast(Predicate<? super T> predicate)
Returns the last element of this which satisfies the given predicate.
|
<U> Traversable<U> |
flatMap(Function<? super T,? extends Iterable<? extends U>> mapper)
FlatMaps this Traversable.
|
default <U> U |
foldLeft(U zero,
BiFunction<? super U,? super T,? extends U> f)
Folds this elements from the left, starting with
zero and successively calling combine . |
<U> U |
foldRight(U zero,
BiFunction<? super T,? super U,? extends U> f)
Folds this elements from the right, starting with
zero and successively calling combine . |
default void |
forEachWithIndex(ObjIntConsumer<? super T> action)
Performs an action on each element.
|
default T |
get()
Gets the first value in iteration order if this
Traversable is not empty, otherwise throws. |
<C> Map<C,? extends Traversable<T>> |
groupBy(Function<? super T,? extends C> classifier)
Groups this elements by classifying the elements.
|
Iterator<? extends Traversable<T>> |
grouped(int size)
Groups this
Traversable into fixed size blocks. |
boolean |
hasDefiniteSize()
Checks if this Traversable is known to have a finite size.
|
int |
hashCode()
Returns the hash code of this collection.
|
T |
head()
Returns the first element of a non-empty Traversable.
|
default Option<T> |
headOption()
Returns the first element of a non-empty Traversable as
Option . |
Traversable<T> |
init()
Dual of tail(), returning all elements except the last.
|
default Option<? extends Traversable<T>> |
initOption()
Dual of tailOption(), returning all elements except the last as
Option . |
default boolean |
isDistinct()
Checks if this Traversable may consist of distinct elements only.
|
default boolean |
isEmpty()
Checks if this Traversable is empty.
|
default boolean |
isOrdered()
Checks if this Traversable is ordered
|
default boolean |
isSequential()
Checks if the elements of this Traversable appear in encounter order.
|
default boolean |
isSingleValued()
Each of Vavr's collections may contain more than one element.
|
boolean |
isTraversableAgain()
Checks if this Traversable can be repeatedly traversed.
|
default Iterator<T> |
iterator()
An iterator by means of head() and tail().
|
T |
last()
Dual of head(), returning the last element.
|
default Option<T> |
lastOption()
Dual of headOption(), returning the last element as
Option . |
int |
length()
Computes the number of elements of this Traversable.
|
<U> Traversable<U> |
map(Function<? super T,? extends U> mapper)
Maps the elements of this
Traversable to elements of a new type preserving their order, if any. |
default Option<T> |
max()
Calculates the maximum of this elements according to their natural order.
|
default Option<T> |
maxBy(Comparator<? super T> comparator)
Calculates the maximum of this elements using a specific comparator.
|
default <U extends Comparable<? super U>> |
maxBy(Function<? super T,? extends U> f)
Calculates the maximum of this elements within the co-domain of a specific function.
|
default Option<T> |
min()
Calculates the minimum of this elements according to their natural order in O(n).
|
default Option<T> |
minBy(Comparator<? super T> comparator)
Calculates the minimum of this elements using a specific comparator.
|
default <U extends Comparable<? super U>> |
minBy(Function<? super T,? extends U> f)
Calculates the minimum of this elements within the co-domain of a specific function.
|
default CharSeq |
mkCharSeq()
Joins the elements of this by concatenating their string representations.
|
default CharSeq |
mkCharSeq(CharSequence delimiter)
Joins the string representations of this elements using a specific delimiter.
|
default CharSeq |
mkCharSeq(CharSequence prefix,
CharSequence delimiter,
CharSequence suffix)
Joins the string representations of this elements using a specific delimiter, prefix and suffix.
|
default String |
mkString()
Joins the elements of this by concatenating their string representations.
|
default String |
mkString(CharSequence delimiter)
Joins the string representations of this elements using a specific delimiter.
|
default String |
mkString(CharSequence prefix,
CharSequence delimiter,
CharSequence suffix)
Joins the string representations of this elements using a specific delimiter, prefix and suffix.
|
static <T> Traversable<T> |
narrow(Traversable<? extends T> traversable)
Narrows a widened
Traversable<? extends T> to Traversable<T>
by performing a type-safe cast. |
default boolean |
nonEmpty()
Checks, this
Traversable is not empty. |
Traversable<T> |
orElse(Iterable<? extends T> other)
Returns this
Traversable if it is nonempty, otherwise return the alternative. |
Traversable<T> |
orElse(Supplier<? extends Iterable<? extends T>> supplier)
Returns this
Traversable if it is nonempty, otherwise return the result of evaluating supplier. |
Tuple2<? extends Traversable<T>,? extends Traversable<T>> |
partition(Predicate<? super T> predicate)
Creates a partition of this
Traversable by splitting this elements in two in distinct traversables
according to a predicate. |
Traversable<T> |
peek(Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
default Number |
product()
Calculates the product of this elements.
|
default T |
reduceLeft(BiFunction<? super T,? super T,? extends T> op)
Accumulates the elements of this Traversable by successively calling the given operation
op from the left. |
default Option<T> |
reduceLeftOption(BiFunction<? super T,? super T,? extends T> op)
Shortcut for
isEmpty() ? Option.none() : Option.some(reduceLeft(op)) . |
default T |
reduceRight(BiFunction<? super T,? super T,? extends T> op)
Accumulates the elements of this Traversable by successively calling the given operation
op from the right. |
default Option<T> |
reduceRightOption(BiFunction<? super T,? super T,? extends T> op)
Shortcut for
isEmpty() ? Option.none() : Option.some(reduceRight(op)) . |
default Traversable<T> |
reject(Predicate<? super T> predicate)
Returns a new traversable consisting of all elements which do not satisfy the given predicate.
|
Traversable<T> |
replace(T currentElement,
T newElement)
Replaces the first occurrence (if exists) of the given currentElement with newElement.
|
Traversable<T> |
replaceAll(T currentElement,
T newElement)
Replaces all occurrences of the given currentElement with newElement.
|
Traversable<T> |
retainAll(Iterable<? extends T> elements)
Keeps all occurrences of the given elements from this.
|
Traversable<T> |
scan(T zero,
BiFunction<? super T,? super T,? extends T> operation)
Computes a prefix scan of the elements of the collection.
|
<U> Traversable<U> |
scanLeft(U zero,
BiFunction<? super U,? super T,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going left to right.
|
<U> Traversable<U> |
scanRight(U zero,
BiFunction<? super T,? super U,? extends U> operation)
Produces a collection containing cumulative results of applying the
operator going right to left.
|
default T |
single()
Returns the single element of this Traversable or throws, if this is empty or contains more than one element.
|
default Option<T> |
singleOption()
Returns the only element of a Traversable as
Option . |
default int |
size()
Computes the number of elements of this Traversable.
|
Iterator<? extends Traversable<T>> |
slideBy(Function<? super T,?> classifier)
Slides a non-overlapping window of a variable size over this
Traversable . |
Iterator<? extends Traversable<T>> |
sliding(int size)
Slides a window of a specific
size and step size 1 over this Traversable by calling
sliding(int, int) . |
Iterator<? extends Traversable<T>> |
sliding(int size,
int step)
Slides a window of a specific
size and step size over this Traversable . |
Tuple2<? extends Traversable<T>,? extends Traversable<T>> |
span(Predicate<? super T> 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. |
default Spliterator<T> |
spliterator() |
default Number |
sum()
Calculates the sum of this elements.
|
Traversable<T> |
tail()
Drops the first element of a non-empty Traversable.
|
Option<? extends Traversable<T>> |
tailOption()
Drops the first element of a non-empty Traversable and returns an
Option . |
Traversable<T> |
take(int n)
Takes the first n elements of this or all elements, if this length < n.
|
Traversable<T> |
takeRight(int n)
Takes the last n elements of this or all elements, if this length < n.
|
Traversable<T> |
takeUntil(Predicate<? super T> predicate)
Takes elements until the predicate holds for the current element.
|
Traversable<T> |
takeWhile(Predicate<? super T> predicate)
Takes elements while the predicate holds for the current element.
|
<T1,T2> Tuple2<? extends Traversable<T1>,? extends Traversable<T2>> |
unzip(Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct
sets.
|
<T1,T2,T3> Tuple3<? extends Traversable<T1>,? extends Traversable<T2>,? extends Traversable<T3>> |
unzip3(Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
Unzips this elements by mapping this elements to triples which are subsequently split into three distinct
sets.
|
<U> Traversable<Tuple2<T,U>> |
zip(Iterable<? extends U> that)
Returns a traversable formed from this traversable and another Iterable collection by combining
corresponding elements in pairs.
|
<U> Traversable<Tuple2<T,U>> |
zipAll(Iterable<? extends U> that,
T thisElem,
U thatElem)
Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in
pairs.
|
<U,R> Traversable<R> |
zipWith(Iterable<? extends U> that,
BiFunction<? super T,? super U,? extends R> mapper)
Returns a traversable formed from this traversable and another Iterable collection by mapping elements.
|
Traversable<Tuple2<T,Integer>> |
zipWithIndex()
Zips this traversable with its indices.
|
<U> Traversable<U> |
zipWithIndex(BiFunction<? super T,? super Integer,? extends U> mapper)
Returns a traversable formed from this traversable and another Iterable collection by mapping elements.
|
fold, reduce, reduceOption
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, isAsync, isLazy, narrow, out, out, stderr, stdout, stringPrefix, 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, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
static <T> Traversable<T> narrow(Traversable<? extends T> traversable)
Traversable<? extends T>
to Traversable<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Traversable
.traversable
- An Traversable
.traversable
instance as narrowed type Traversable<T>
.default <K> Option<Map<K,T>> arrangeBy(Function<? super T,? extends K> getKey)
None
.K
- key class typegetKey
- A function which extracts a key from elementsNullPointerException
- if getKey
is null.groupBy(Function)
default Option<Double> average()
Number
.
Since we do not know if the component type T
is of type Number
, the
average()
call might throw at runtime (see examples below).
Examples:
List.empty().average() // = None
List.of(1, 2, 3).average() // = Some(2.0)
List.of(1.0, 10e100, 2.0, -10e100).average() // = Some(0.75)
List.of(1.0, Double.NaN).average() // = NaN
List.of("apple", "pear").average() // throws
Please note that Java's DoubleStream.average()
uses the
Kahan summation algorithm
(also known as compensated summation), which has known limitations.
Vavr uses Neumaier's modification of the Kahan algorithm, which yields better results.
// = OptionalDouble(0.0) (wrong)
j.u.s.DoubleStream.of(1.0, 10e100, 2.0, -10e100).average()
// = Some(0.75) (correct)
List.of(1.0, 10e100, 2.0, -10e100).average()
Some(average)
or None
, if there are no elementsUnsupportedOperationException
- if this elements are not numeric<R> Traversable<R> collect(PartialFunction<? super T,? extends R> partialFunction)
partialFunction
by mapping the elements to type R
.
More specifically, for each of this elements in iteration order first it is checked
partialFunction.isDefinedAt(element)
If the elements makes it through that filter, the mapped instance is added to the result collection
R newElement = partialFunction.apply(element)
Note:If this Traversable
is ordered (i.e. extends Ordered
,
the caller of collect
has to ensure that the elements are comparable (i.e. extend Comparable
).R
- The new element typepartialFunction
- A function that is not necessarily defined of all elements of this traversable.Traversable
instance containing elements of type R
NullPointerException
- if partialFunction
is nulldefault boolean containsAll(Iterable<? extends T> elements)
The result is equivalent to
elements.isEmpty() ? true : contains(elements.head()) && containsAll(elements.tail())
but implemented
without recursion.
elements
- A List of values of type T.NullPointerException
- if elements
is nulldefault int count(Predicate<? super T> predicate)
predicate
- A predicate>= 0
NullPointerException
- if predicate
is null.Traversable<T> distinct()
equals
.Traversable
containing this elements without duplicatesTraversable<T> distinctBy(Comparator<? super T> comparator)
comparator
.comparator
- A comparatorTraversable
containing this elements without duplicatesNullPointerException
- if comparator
is null.<U> Traversable<T> distinctBy(Function<? super T,? extends U> keyExtractor)
equals
.
The elements of the result are determined in the order of their occurrence - first match wins.
U
- key typekeyExtractor
- A key extractorTraversable
containing this elements without duplicatesNullPointerException
- if keyExtractor
is nullTraversable<T> drop(int n)
n
- The number of elements to drop.Traversable<T> dropRight(int n)
n
- The number of elements to drop.Traversable<T> dropUntil(Predicate<? super T> predicate)
predicate
- A condition tested subsequently for this elements.NullPointerException
- if predicate
is nullTraversable<T> dropWhile(Predicate<? super T> predicate)
Note: This is essentially the same as dropUntil(predicate.negate())
.
It is intended to be used with method references, which cannot be negated directly.
predicate
- A condition tested subsequently for this elements.NullPointerException
- if predicate
is nullboolean equals(Object obj)
Notes:
default boolean existsUnique(Predicate<? super T> predicate)
predicate
- A PredicateNullPointerException
- if predicate
is nullTraversable<T> filter(Predicate<? super T> predicate)
predicate
- A predicateNullPointerException
- if predicate
is nulldefault Traversable<T> reject(Predicate<? super T> predicate)
The default implementation is equivalent to
filter(predicate.negate()
predicate
- A predicateNullPointerException
- if predicate
is nulldefault Option<T> find(Predicate<? super T> predicate)
predicate
- A predicate.List.of(null).find(e -> e == null)
).NullPointerException
- if predicate
is nulldefault Option<T> findLast(Predicate<? super T> predicate)
Same as reverse().find(predicate)
.
predicate
- A predicate.List.of(null).find(e -> e == null)
).NullPointerException
- if predicate
is null<U> Traversable<U> flatMap(Function<? super T,? extends Iterable<? extends U>> mapper)
U
- The resulting component type.mapper
- A mapperdefault <U> U foldLeft(U zero, BiFunction<? super U,? super T,? extends U> f)
Foldable
zero
and successively calling combine
.
Example:
// = "cba!"
List("a", "b", "c").foldLeft("!", (xs, x) -> x + xs)
<U> U foldRight(U zero, BiFunction<? super T,? super U,? extends U> f)
Foldable
zero
and successively calling combine
.
Example:
// = "!cba"
List("a", "b", "c").foldRight("!", (x, xs) -> xs + x)
default void forEachWithIndex(ObjIntConsumer<? super T> action)
Value.forEach(Consumer)
,
additionally the element's index is passed to the given action
.
This is essentially the same as iterator().zipWithIndex().forEach()
but performs better because
no intermediate Tuple2
instances are created and no boxing of int values takes place.
Please note that subsequent calls to forEachWithIndex
might lead to different iteration orders,
depending on the underlying Traversable
implementation.
Please also note that forEachWithIndex
might loop infinitely if the Traversable
is lazily
evaluated, like Stream
.
action
- A ObjIntConsumer
NullPointerException
- if action
is nulldefault T get()
Traversable
is not empty, otherwise throws.get
in interface Value<T>
NoSuchElementException
- if this Traversable
is empty.<C> Map<C,? extends Traversable<T>> groupBy(Function<? super T,? extends C> classifier)
C
- classified class typeclassifier
- A function which classifies elements into classesNullPointerException
- if classifier
is null.arrangeBy(Function)
Iterator<? extends Traversable<T>> grouped(int size)
Traversable
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 sliding(int, int), i.e.
grouped(size)
is the same as sliding(size, size)
.size
- a positive block sizeIllegalArgumentException
- if size
is negative or zeroboolean hasDefiniteSize()
This method should be implemented by classes only, i.e. not by interfaces.
T head()
NoSuchElementException
- if this is emptydefault Option<T> headOption()
Option
.Some(element)
or None
if this is empty.int hashCode()
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()) + ")";
}
}
Traversable<T> init()
UnsupportedOperationException
- if this is emptydefault Option<? extends Traversable<T>> initOption()
Option
.Some(traversable)
or None
if this is empty.default boolean isDistinct()
default boolean isEmpty()
default boolean isOrdered()
default boolean isSequential()
default boolean isSingleValued()
isSingleValued
in interface Value<T>
false
boolean isTraversableAgain()
This method should be implemented by classes only, i.e. not by interfaces.
default Iterator<T> iterator()
T last()
NoSuchElementException
- is this is emptydefault Option<T> lastOption()
Option
.Some(element)
or None
if this is empty.int length()
Same as size()
.
<U> Traversable<U> map(Function<? super T,? extends U> mapper)
Traversable
to elements of a new type preserving their order, if any.map
in interface Value<T>
U
- Component type of the target Traversablemapper
- A mapper.NullPointerException
- if mapper
is nulldefault Option<T> max()
Examples:
List.empty().max() // = None
List.of(1, 2, 3).max() // = Some(3)
List.of("a", "b", "c").max() // = Some("c")
List.of(1.0, Double.NaN).max() // = NaN
List.of(1, "a").max() // throws
Some(maximum)
of this elements or None
if this is emptyNullPointerException
- if an element is nullClassCastException
- if the elements do not have a natural order, i.e. they do not implement Comparabledefault Option<T> maxBy(Comparator<? super T> comparator)
comparator
- A non-null element comparatorSome(maximum)
of this elements or None
if this is emptyNullPointerException
- if comparator
is nulldefault <U extends Comparable<? super U>> Option<T> maxBy(Function<? super T,? extends U> f)
U
- The type where elements are comparedf
- A function that maps this elements to comparable elementsNullPointerException
- if f
is null.default Option<T> min()
Examples:
List.empty().min() // = None
List.of(1, 2, 3).min() // = Some(1)
List.of("a", "b", "c").min() // = Some("a")
List.of(1.0, Double.NaN).min() // = NaN
List.of(1, "a").min() // throws
There is an exception for Double
and Float
: The minimum is defined to be NaN
if
this contains NaN
. According to the natural order NaN
would be the maximum element
instead.Some(minimum)
of this elements or None
if this is emptyNullPointerException
- if an element is nullClassCastException
- if the elements do not have a natural order, i.e. they do not implement Comparabledefault Option<T> minBy(Comparator<? super T> comparator)
comparator
- A non-null element comparatorSome(minimum)
of this elements or None
if this is emptyNullPointerException
- if comparator
is nulldefault <U extends Comparable<? super U>> Option<T> minBy(Function<? super T,? extends U> f)
U
- The type where elements are comparedf
- A function that maps this elements to comparable elementsNullPointerException
- if f
is null.default CharSeq mkCharSeq()
This has the same effect as calling mkCharSeq("", "", "")
.
CharSeq
default CharSeq mkCharSeq(CharSequence delimiter)
This has the same effect as calling mkCharSeq("", delimiter, "")
.
delimiter
- A delimiter string put between string representations of elements of thisCharSeq
default CharSeq mkCharSeq(CharSequence prefix, CharSequence delimiter, CharSequence suffix)
Example: List.of("a", "b", "c").mkCharSeq("Chars(", ", ", ")") = CharSeq.of("Chars(a, b, c))"
default String mkString()
This has the same effect as calling mkString("", "", "")
.
default String mkString(CharSequence delimiter)
This has the same effect as calling mkString("", delimiter, "")
.
delimiter
- A delimiter string put between string representations of elements of thisdefault String mkString(CharSequence prefix, CharSequence delimiter, CharSequence suffix)
Example: List.of("a", "b", "c").mkString("Chars(", ", ", ")") = "Chars(a, b, c)"
prefix
- prefix of the resulting stringdelimiter
- A delimiter string put between string representations of elements of thissuffix
- suffix of the resulting stringdefault boolean nonEmpty()
Traversable
is not empty.
The call is equivalent to !isEmpty()
.
Traversable<T> orElse(Iterable<? extends T> other)
Traversable
if it is nonempty, otherwise return the alternative.other
- An alternative Traversable
Traversable
if it is nonempty, otherwise return the alternative.Traversable<T> orElse(Supplier<? extends Iterable<? extends T>> supplier)
Traversable
if it is nonempty, otherwise return the result of evaluating supplier.supplier
- An alternative Traversable
supplierTraversable
if it is nonempty, otherwise return the result of evaluating supplier.Tuple2<? extends Traversable<T>,? extends Traversable<T>> partition(Predicate<? super T> predicate)
Traversable
by splitting this elements in two in distinct traversables
according to a predicate.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.NullPointerException
- if predicate is nullTraversable<T> peek(Consumer<? super T> action)
Value
action
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.default Number product()
Byte
, Double
, Float
,
Integer
, Long
, Short
, BigInteger
and BigDecimal
.
Examples:
List.empty().product() // = 1
List.of(1, 2, 3).product() // = 6L
List.of(0.1, 0.2, 0.3).product() // = 0.006
List.of("apple", "pear").product() // throws
Please also see Foldable.fold(Object, BiFunction)
, a way to do a type-safe multiplication of elements.Number
representing the sum of this elementsUnsupportedOperationException
- if this elements are not numericdefault T reduceLeft(BiFunction<? super T,? super T,? extends T> op)
op
from the left.reduceLeft
in interface Foldable<T>
op
- A BiFunction of type TNoSuchElementException
- if this is emptyNullPointerException
- if op
is nulldefault Option<T> reduceLeftOption(BiFunction<? super T,? super T,? extends T> op)
isEmpty() ? Option.none() : Option.some(reduceLeft(op))
.reduceLeftOption
in interface Foldable<T>
op
- A BiFunction of type TNullPointerException
- if op
is nulldefault T reduceRight(BiFunction<? super T,? super T,? extends T> op)
op
from the right.reduceRight
in interface Foldable<T>
op
- An operation of type TNoSuchElementException
- if this is emptyNullPointerException
- if op
is nulldefault Option<T> reduceRightOption(BiFunction<? super T,? super T,? extends T> op)
isEmpty() ? Option.none() : Option.some(reduceRight(op))
.reduceRightOption
in interface Foldable<T>
op
- An operation of type TNullPointerException
- if op
is nullTraversable<T> replace(T currentElement, T newElement)
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.Traversable<T> replaceAll(T currentElement, T newElement)
currentElement
- An element to be substituted.newElement
- A replacement for currentElement.Traversable<T> retainAll(Iterable<? extends T> elements)
elements
- Elements to be kept.NullPointerException
- if elements
is nullTraversable<T> scan(T zero, BiFunction<? super T,? super T,? extends T> operation)
zero
- neutral element for the operator opoperation
- the associative operator for the scanNullPointerException
- if operation
is null.<U> Traversable<U> scanLeft(U zero, BiFunction<? super U,? super T,? extends U> operation)
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementNullPointerException
- if operation
is null.<U> Traversable<U> scanRight(U zero, BiFunction<? super T,? super U,? extends U> operation)
U
- the type of the elements in the resulting collectionzero
- the initial valueoperation
- the binary operator applied to the intermediate result and the elementNullPointerException
- if operation
is null.default T single()
NoSuchElementException
- if the Traversable does not contain a single element.default Option<T> singleOption()
Option
.Some(element)
or None
if the Traversable does not contain a single element.default int size()
Same as length()
.
Iterator<? extends Traversable<T>> slideBy(Function<? super T,?> classifier)
Traversable
.
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]]
classifier
- A function which classifies elements into classesNullPointerException
- if classifier
is null.Iterator<? extends Traversable<T>> sliding(int size)
size
and step size 1 over this Traversable
by calling
sliding(int, int)
.size
- a positive window sizeIllegalArgumentException
- if size
is negative or zeroIterator<? extends Traversable<T>> sliding(int size, int step)
size
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],[4]]
size
- a positive window sizestep
- a positive step sizeIllegalArgumentException
- if size
or step
are negative or zeroTuple2<? extends Traversable<T>,? extends Traversable<T>> span(Predicate<? super T> predicate)
predicate
and the second element is the remainder.predicate
- A predicate.Tuple
containing the longest prefix of elements that satisfy p and the remainder.NullPointerException
- if predicate
is nulldefault Spliterator<T> spliterator()
spliterator
in interface Iterable<T>
spliterator
in interface Value<T>
default Number sum()
Byte
, Double
, Float
,
Integer
, Long
, Short
, BigInteger
and BigDecimal
.
Examples:
List.empty().sum() // = 0
List.of(1, 2, 3).sum() // = 6L
List.of(0.1, 0.2, 0.3).sum() // = 0.6
List.of("apple", "pear").sum() // throws
Please also see Foldable.fold(Object, BiFunction)
, a way to do a type-safe summation of elements.Number
representing the sum of this elementsUnsupportedOperationException
- if this elements are not numericTraversable<T> tail()
UnsupportedOperationException
- if this is emptyOption<? extends Traversable<T>> tailOption()
Option
.Some(traversable)
or None
if this is empty.Traversable<T> take(int n)
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.
n
- The number of elements to take.Traversable<T> takeRight(int n)
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.
n
- The number of elements to take.Traversable<T> takeUntil(Predicate<? super T> predicate)
Note: This is essentially the same as takeWhile(predicate.negate())
. It is intended to be used with
method references, which cannot be negated directly.
predicate
- A condition tested subsequently for this elements.NullPointerException
- if predicate
is nullTraversable<T> takeWhile(Predicate<? super T> predicate)
predicate
- A condition tested subsequently for the contained elements.NullPointerException
- if predicate
is null<T1,T2> Tuple2<? extends Traversable<T1>,? extends Traversable<T2>> unzip(Function<? super T,Tuple2<? extends T1,? extends T2>> unzipper)
T1
- 1st element type of a pair returned by unzipperT2
- 2nd element type of a pair returned by unzipperunzipper
- a function which converts elements of this to pairsNullPointerException
- if unzipper
is null<T1,T2,T3> Tuple3<? extends Traversable<T1>,? extends Traversable<T2>,? extends Traversable<T3>> unzip3(Function<? super T,Tuple3<? extends T1,? extends T2,? extends T3>> unzipper)
T1
- 1st element type of a triplet returned by unzipperT2
- 2nd element type of a triplet returned by unzipperT3
- 3rd element type of a triplet returned by unzipperunzipper
- a function which converts elements of this to pairsNullPointerException
- if unzipper
is null<U> Traversable<Tuple2<T,U>> zip(Iterable<? extends U> that)
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.that
iterable.NullPointerException
- if that
is null<U> Traversable<Tuple2<T,U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem)
The length of the returned traversable is the maximum of the lengths of this traversable and that
iterable.
Special case: if this traversable is shorter than that elements, and that elements contains duplicates, the resulting traversable may be shorter than the maximum of the lengths of this and that because a traversable contains an element at most once.
If this Traversable is shorter than that, thisElem values are used to fill the result. If that is shorter than this Traversable, thatElem values are used to fill the result.
U
- The type of the second half of the returned pairs.that
- The Iterable providing the second half of each result pair.thisElem
- The element to be used to fill up the result if this traversable is shorter than that.thatElem
- The element to be used to fill up the result if that is shorter than this traversable.NullPointerException
- if that
is null<U,R> Traversable<R> zipWith(Iterable<? extends U> that, BiFunction<? super T,? super U,? extends R> mapper)
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
U
- The type of the second parameter of the mapper.R
- The type of the mapped elements.that
- The Iterable providing the second parameter of the mapper.mapper
- a mapper.that
iterable.NullPointerException
- if that
or mapper
is nullTraversable<Tuple2<T,Integer>> zipWithIndex()
<U> Traversable<U> zipWithIndex(BiFunction<? super T,? super Integer,? extends U> mapper)
The length of the returned traversable is the minimum of the lengths of this traversable and that
iterable.
U
- The type of the mapped elements.mapper
- a mapper.that
iterable.NullPointerException
- if mapper
is nullCopyright © 2021. All Rights Reserved.