Interface Traversable<T>

  • Type Parameters:
    T -

    @Experimental
    public interface Traversable<T>
    Unsorted traversable stream for sequential and aggregating operations.

    Traversable contains two type of operations:

    1. Intermediate operations which transform a traversable, into another, e.g. filter(Predicate).
    2. Terminal operations which produce a side effect, e.g. forEach(Consumer). Once a terminal operation is completed, the resources taken by the traversable are released.

    Traversable cannot be reused and hence is designed to be used only once via its intermediate and terminal operations.

    In distributed environments, unless individually specified, all lambdas passed to methods are executed where data is located. For example, if executing forEach(Consumer), the Consumer function is executed wherever a particular key resides. To execute a for-each operation where the side effects are executed locally, all the Traversable's data needs to be collected and iterated over manually.

    Since:
    8.0
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      boolean allMatch​(Predicate<? super T> p)
      A terminal operation that returns whether all elements of this traversable match the provided predicate.
      boolean anyMatch​(Predicate<? super T> p)
      A terminal operation that returns whether any elements of this traversable match the provided predicate.
      <R> R collect​(Supplier<R> s, BiConsumer<R,​? super T> accumulator, BiConsumer<R,​R> combiner)
      A terminal operation that transforms the traversable into a result container, first constructed with the given supplier, and then accumulating elements over it with the given consumer.
      <R,​A>
      R
      collect​(Collector<? super T,​A,​R> collector)
      A terminal operation that transforms the traversable into a result container using a Collector.
      long count()
      A terminal operation that returns the number of elements in the traversable.
      Traversable<T> filter​(Predicate<? super T> p)
      An intermediate operation that returns a traversable containing elements matching the given predicate.
      Optional<T> findAny()
      A terminal operation that returns an optional containing an element of the traversable, or an empty optional if empty.
      <R> Traversable<R> flatMap​(Function<? super T,​? extends Traversable<? extends R>> f)
      An intermediate operation that returns a traversable containing the results of replacing each element of this traversable with the contents of a traversable produced by applying the provided function to each element.
      void forEach​(Consumer<? super T> c)
      A terminal operation that applies an operation to all elements of this traversable.
      <R> Traversable<R> map​(Function<? super T,​? extends R> f)
      An intermediate operation that returns a traversable containing the results of applying the given function over the elements of the traversable.
      default Optional<T> max​(Comparator<? super T> comparator)
      A terminal operation that returns an optional containing the maximum element of this traversable based on the comparator passed in.
      default Optional<T> min​(Comparator<? super T> comparator)
      A terminal operation that returns an optional containing the minimum element of this traversable based on the comparator passed in.
      boolean noneMatch​(Predicate<? super T> predicate)
      A terminal operation that returns whether no elements of this traversable match the provided predicate.
      Optional<T> reduce​(BinaryOperator<T> folder)
      A terminal operation that applies a binary folding operation to all elements of this traversable, and wraps the result in an optional.
      T reduce​(T z, BinaryOperator<T> folder)
      A terminal operation that applies a binary folding operation to a start value and all elements of this traversable.
      <U> U reduce​(U z, BiFunction<U,​? super T,​U> mapper, BinaryOperator<U> folder)
      A terminal operation that applies a binary folding operation to a start value and the result of each element having a mapping function applied.
    • Method Detail

      • filter

        Traversable<T> filter​(Predicate<? super T> p)
        An intermediate operation that returns a traversable containing elements matching the given predicate.
      • map

        <R> Traversable<R> map​(Function<? super T,​? extends R> f)
        An intermediate operation that returns a traversable containing the results of applying the given function over the elements of the traversable.
      • flatMap

        <R> Traversable<R> flatMap​(Function<? super T,​? extends Traversable<? extends R>> f)
        An intermediate operation that returns a traversable containing the results of replacing each element of this traversable with the contents of a traversable produced by applying the provided function to each element.

        From a functional map perspective, this operation is particularly handy when the values are collections.

      • forEach

        void forEach​(Consumer<? super T> c)
        A terminal operation that applies an operation to all elements of this traversable.
      • reduce

        T reduce​(T z,
                 BinaryOperator<T> folder)
        A terminal operation that applies a binary folding operation to a start value and all elements of this traversable.
      • reduce

        Optional<T> reduce​(BinaryOperator<T> folder)
        A terminal operation that applies a binary folding operation to all elements of this traversable, and wraps the result in an optional. If the traversable is empty, it returns an empty optional.
      • reduce

        <U> U reduce​(U z,
                     BiFunction<U,​? super T,​U> mapper,
                     BinaryOperator<U> folder)
        A terminal operation that applies a binary folding operation to a start value and the result of each element having a mapping function applied.

        This is a combined map/reduce which could potentially be done more efficiently than if a map is executed and then reduce.

      • collect

        <R> R collect​(Supplier<R> s,
                      BiConsumer<R,​? super T> accumulator,
                      BiConsumer<R,​R> combiner)
        A terminal operation that transforms the traversable into a result container, first constructed with the given supplier, and then accumulating elements over it with the given consumer.

        The combiner can be used to combine accumulated results executed in parallel or coming from different nodes in a distributed environment.

        In distributed environments where some keys are remote, the Supplier and BiConsumer instances passed in are sent to other nodes and hence they need to be marshallable. If the collect operation can be defined using the helper methods in Collectors, it is recommended that those are used, which can easily be made marshalled using the org.infinispan.stream.CacheCollectors#serializableCollector method.

      • collect

        <R,​A> R collect​(Collector<? super T,​A,​R> collector)
        A terminal operation that transforms the traversable into a result container using a Collector.

        In distributed environments where some keys are remote, the Collector instance passed in is sent other nodes and hence it needs to be marshallable. This can easily be made achieved using the org.infinispan.stream.CacheCollectors#serializableCollector method.

      • min

        default Optional<T> min​(Comparator<? super T> comparator)
        A terminal operation that returns an optional containing the minimum element of this traversable based on the comparator passed in. If the traversable is empty, it returns an empty optional.
      • max

        default Optional<T> max​(Comparator<? super T> comparator)
        A terminal operation that returns an optional containing the maximum element of this traversable based on the comparator passed in. If the traversable is empty, it returns an empty optional.
      • count

        long count()
        A terminal operation that returns the number of elements in the traversable.
      • anyMatch

        boolean anyMatch​(Predicate<? super T> p)
        A terminal operation that returns whether any elements of this traversable match the provided predicate.

        An important reason to keep this method is the fact as opposed to a reduction which must evaluate all elements in the traversable, this method could stop as soon as it has found an element that matches.

      • allMatch

        boolean allMatch​(Predicate<? super T> p)
        A terminal operation that returns whether all elements of this traversable match the provided predicate.

        An important reason to keep this method is the fact as opposed to a reduction which must evaluate all elements in the traversable, this method could stop as soon as it has found an element that does not match the predicate.

      • noneMatch

        boolean noneMatch​(Predicate<? super T> predicate)
        A terminal operation that returns whether no elements of this traversable match the provided predicate.

        An important reason to keep this method is the fact as opposed to a reduction which must evaluate all elements in the traversable, this method could stop as soon as it has found an element that does matches the predicate.

      • findAny

        Optional<T> findAny()
        A terminal operation that returns an optional containing an element of the traversable, or an empty optional if empty.