Interface Traversable<T>

All Superinterfaces:
Iterable<T>
All Known Subinterfaces:
MutableCollection<T>
All Known Implementing Classes:
Collection, List, MutableList, MutableSet, Opt, Set

public interface Traversable<@Out T> extends Iterable<T>
Collection of elements that may be traversed in sequence. This interface adds transformation and reduction methods to the Iterable. The transformations return traversable views of the collection indicating that their result should only be used for reduction or iteration. Traversables can be chained avoiding iteration until the reduction is performed.
  • Method Details

    • takeWhile

      default Traversable<T> takeWhile(Predicate<? super @Out T> condition)
      Iterates through elements of this traversable until given condition fails. The returned traversable is a view of this object.
      See Also:
    • take

      default Traversable<T> take(int limit)
      Traverses no more than limit elements of this traversable. The returned traversable is a view of this object.
      See Also:
    • filter

      default Traversable<T> filter(Predicate<? super @Out T> condition)
      Returns traversable that only traverses elements satisfying given condition. The returned traversable is a view of this object.
      See Also:
    • narrow

      default <U> Traversable<U> narrow(Class<U> clazz)
      Returns traversable that only traverses subclasses of clazz. The returned traversable is a view of this object.
    • map

      default <R> Traversable<R> map(Function<? super @Out T,? extends R> mapping)
      Returns traversable with given mapping applied to all elements. The returned traversable is a view of this traversable that lazily applies mapping to elements returned by iterator.
      See Also:
    • chain

      default Traversable<?> chain(Traversable<?> other)
      Traversable that iterates over elements of both traversables. The iterator of the result first returns elements of this traverable, and once it is exhausted elements of the other traversable.
      See Also:
    • chainMap

      default <R> Traversable<R> chainMap(Function<? super @Out T,? extends Traversable<R>> mapping)
      Applies mapping to the elements and chains the resulting traversables together.
    • first

      default T first() throws NoSuchElementException
      Returns the first element produced by the iterator of this traversable. May produce different results each time it is called if the traversable has no defined order of its elements.
      Throws:
      NoSuchElementException - if the traversable is empty.
    • exists

      default boolean exists(Predicate<? super @Out T> condition)
      Tests whether any element in this traversable matches given condition.
    • forall

      default boolean forall(Predicate<? super @Out T> condition)
      Tests whether all elements in this traversable match given condition.
    • fold

      default <R> R fold(R first, BiFunction<? super R,? super @Out T,? extends R> combine)
      Starting from the first, applies combine to elements of the collection returning result. The order of elements is determined by the iterator.
    • find

      default Opt<T> find(Predicate<? super @Out T> condition)
      Returns element that satisfies condition or empty optional if there is no such element.
    • join

      default String join(String start, String separator, String end)
      Joins elements of the traversable into a string.
    • join

      default String join(String separator)
      Joins elements of the traversable into a string.
    • count

      default int count(Predicate<? super @Out T> condition)
      Counts number of elements that satisfy given condition. If the traversable has more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
      Since:
      4.1
    • count

      default int count()
      Counts number of elements in this collection. As opposed to Collection.size() this method may need to perform computations to count the number of elements. If the traversable has more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
    • isEmpty

      default boolean isEmpty()
      Whether this collection has no elements.
    • nonEmpty

      default boolean nonEmpty()
      Whether this collection contains elements.
    • groupedBy

      @Deprecated(since="4.1") default <K> Map<K,? extends List<T>> groupedBy(Function<? super @Out T,? extends K> classifier)
      Collects elements of this traversable into lists grouped by given classifier. The resulting collection is immutable and is not affected by changes to this traversable.
      Type Parameters:
      K - type of the classifier key.
      Parameters:
      classifier - function that assigns keys to elements of this traversable.
    • groupedIntoSets

      default <K> SetMultimap<K,T> groupedIntoSets(Function<? super @Out T,? extends K> classifier)
      Collects elements of this traversable into sets grouped by given classifier. The resulting collection is immutable and is not affected by changes to this traversable.
      Type Parameters:
      K - type of the classifier key.
      Parameters:
      classifier - function that assigns keys to elements of this traversable.
      Since:
      4.1
    • groupedIntoLists

      default <K> ListMultimap<K,T> groupedIntoLists(Function<? super @Out T,? extends K> classifier)
      Collects elements of this traversable into sets grouped by given classifier. The resulting collection is immutable and is not affected by changes to this traversable.
      Type Parameters:
      K - type of the classifier key.
      Parameters:
      classifier - function that assigns keys to elements of this traversable.
      Since:
      4.1
    • sortedBy

      default List<T> sortedBy(Comparator<? super @Out T> comparing)
      Returns list of elements of this traversable sorted by given comparator.
      Parameters:
      comparing - comparator for elements of this traversable.
    • toList

      default List<T> toList()
      Collects elements of this traversable into an immutable list.
    • toSet

      default Set<T> toSet()
      Collects elements of this traversable into an immutable set.