Class KiwiStreams

java.lang.Object
org.kiwiproject.collect.KiwiStreams

public final class KiwiStreams extends Object
Utilities related to Streams that are not already in the JDKs Stream or Guava's Streams.
  • Method Details

    • findFirst

      public static <T> Optional<T> findFirst(Stream<?> stream, Class<T> typeToFind)
      Find the first object having the given typeToFind in a stream of objects.
      Type Parameters:
      T - the type token of the type we want to find
      Parameters:
      stream - the stream of objects of some (unknown) type
      typeToFind - the class of the object to find
      Returns:
      an Optional containing the first object of the given type, or empty
    • findFirst

      public static <T> Optional<T> findFirst(Stream<?> stream, Class<T> typeToFind, Predicate<T> predicate)
      Find the first object having the given typeToFind and matching the supplied predicate in a stream of objects.
      Type Parameters:
      T - the type token of the type we want to find
      Parameters:
      stream - the stream of objects of some (unknown) type
      typeToFind - the class of the object to find
      predicate - the condition that must be satisfied for a match to occur
      Returns:
      an Optional containing the first object of the given type, or empty
    • stream

      public static <T> Stream<T> stream(Collection<T> collection, KiwiStreams.StreamMode mode)
      Return a sequential or parallel Stream over collection based on the given KiwiStreams.StreamMode.

      Normally you must hard code the type of stream directly in the code using either Collection.stream() or Collection.parallelStream(). This method provides a simple way to determine how to process a stream at runtime, for example based on number of elements or type of algorithm.

      Type Parameters:
      T - the type of collection elements
      Parameters:
      collection - the collection to stream
      mode - the mode to use when streaming the collection
      Returns:
      a sequential or parallel Stream over collection