Class StreamExtras

java.lang.Object
dev.bannmann.labs.core.StreamExtras

public final class StreamExtras extends Object
  • Method Details

    • atMostOne

      public static <T extends @Nullable Object> BinaryOperator<T> atMostOne()
      Use with Stream.reduce(BinaryOperator) to ensure there is at most one element. Note that if you want an exception in case no element exists, you should instead use Guava's MoreCollectors.onlyElement().
      Type Parameters:
      T - element type
      Returns:
      an operator to use with Stream.reduce(BinaryOperator)
    • atMostOneThrowing

      public static <T extends @Nullable Object, E extends RuntimeException> BinaryOperator<T> atMostOneThrowing(Supplier<E> exception)
      Use with Stream.reduce(BinaryOperator) to ensure there is at most one element. Note that if you want an exception in case no element exists, you should instead use Guava's MoreCollectors.onlyElement().
      Type Parameters:
      T - element type
      E - type of the exception to throw if more than one element is encountered
      Parameters:
      exception - thrown if more than one element is encountered
    • lastN

      public static <T> Collector<T,?,List<T>> lastN(int limit)
      Collects the last n input elements (or fewer) into a list.
      Type Parameters:
      T - element type
      Parameters:
      limit - the maximum number of elements. Must be greater than 0.
      Throws:
      IllegalArgumentException - if limit is 0 or negative