-
- All Superinterfaces:
Foldable<T>,java.lang.Iterable<T>,java.io.Serializable,Streamable<T>,Value<T>
- All Known Subinterfaces:
Collection<T>,Iterator<T>,List<T>,Map<K,T>,Sequence<T>,Set<T>,Tree<T>,Tree.NodeCollection<T>
public interface Traversable<T> extends Value<T>, Foldable<T>
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Optional<java.lang.Double>average()Calculates the average of this elements, assuming that the element type isNumber.Traversable<T>filter(java.util.function.Predicate<T> predicate)Filter out an element if it does not match the suppliedpredicate.Optional<T>first(java.util.function.Predicate<T> predicate)Find the first match in the elements using the providedPredicate.Iterator<T>iterator()Optional<T>last(java.util.function.Predicate<T> predicate)Find the last match in the Iterator using the providedPredicate.<U> Traversable<U>map(java.util.function.Function<T,U> mapper)Perform a mapping operation on the elements in the stream.Traversable<T>orElse(java.lang.Iterable<? extends T> other)Returns eitherthisif it is non empty, otherwise will return the providedother.Traversable<T>orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)Returns eitherthisif it is non empty, otherwise the provided supplier is evaluated and returned.Traversable<T>reject(java.util.function.Predicate<T> predicate)Return a list that removes all elements that match thepredicateprovided.default Optional<java.lang.Double>sum()Calculates the sum of this elements.
-
-
-
Method Detail
-
filter
Traversable<T> filter(java.util.function.Predicate<T> predicate)
Description copied from interface:StreamableFilter out an element if it does not match the suppliedpredicate. This operation will iterate over all elements and return a new set containing only the elements where the predicate returnstruefor.- Specified by:
filterin interfaceStreamable<T>- Parameters:
predicate- the predicate to apply to the contents of this- Returns:
- the filtered value
-
first
Optional<T> first(java.util.function.Predicate<T> predicate)
Find the first match in the elements using the providedPredicate. The returned Optional isnullsafe and will either contain the element or be an emptyOptional.Example:
// the result will be an Optional with the value 2 int firstMatch = Collection(1, 2, 3, 4) .first(i -> i % 2 == 0);- Parameters:
predicate- the predicate to use- Returns:
- the first match found
- Throws:
java.lang.NullPointerException- in case that the predicate is null
-
last
Optional<T> last(java.util.function.Predicate<T> predicate)
Find the last match in the Iterator using the providedPredicate. The returned Optional isnullsafe and will either contain the element or be an emptyOptional.Example:
// the result will be an Optional with the value 4 int firstMatch = Collection(1, 2, 3, 4) .last(i -> i % 2 == 0);- Parameters:
predicate- the predicate to use- Returns:
- the last match found
- Throws:
java.lang.NullPointerException- in case that the predicate is null
-
map
<U> Traversable<U> map(java.util.function.Function<T,U> mapper)
Description copied from interface:StreamablePerform a mapping operation on the elements in the stream.This operation will loop over all elements in the stream and apply the
mappermethod. The mapped values will be returned in as a new stream of elements.- Specified by:
mapin interfaceStreamable<T>- Type Parameters:
U- the type of object expected as a result- Parameters:
mapper- the mapping functionality- Returns:
- the mapped object
-
orElse
Traversable<T> orElse(java.lang.Iterable<? extends T> other)
Returns eitherthisif it is non empty, otherwise will return the providedother.- Parameters:
other- the alternative- Returns:
- this
Traversableif non empty, orother
-
orElse
Traversable<T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Returns eitherthisif it is non empty, otherwise the provided supplier is evaluated and returned.- Parameters:
supplier- the supplier to generate the other- Returns:
- this (@code Traversable} if non empty, otherwise other
-
reject
Traversable<T> reject(java.util.function.Predicate<T> predicate)
Return a list that removes all elements that match thepredicateprovided.- Parameters:
predicate- the predicate to use- Returns:
- the elements that do not match the
predicate - Throws:
java.lang.NullPointerException- in casepredicateis null
-
sum
default Optional<java.lang.Double> sum()
Calculates the sum of this elements. Supported component types areByte,Double,Float,Integer,Long,Short,BigIntegerandBigDecimal.Examples:
API.List().sum() // = Optional() API.List(1, 2, 3).sum() // = Optional(6.0) API.List(1.0, 10e100, 2.0, -10e100).sum() // = Optional(3.0) API.List(1.0, Double.NaN).sum() // = NaN API.List("apple", "pear").sum() // throws an exception- Returns:
Optional(sum), orOptional()if no elements are present- Throws:
java.lang.ClassCastException- if the elements are not numeric
-
average
default Optional<java.lang.Double> average()
Calculates the average of this elements, assuming that the element type isNumber. Since we do not know if the component typeTis of typeNumber, theaverage()call might throw at runtime (see examples below).Examples
API.List().average() // = Optional() API.List(1, 2, 3).average() // = Optional(2.0) API.List(1.0, 10e100, 2.0, -10e100).average() // = Optional(0.75) API.List(1.0, Double.NaN).average() // = NaN API.List("apple", "pear").average() // throws an exception- Returns:
Optional(average), or {@code Optional()} if no elements are present- Throws:
java.lang.ClassCastException- if the elements are not numeric
-
-