Class StreamExtras


  • public final class StreamExtras
    extends Object
    • Method Detail

      • atMostOne

        public static <T> 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,​E extends RuntimeExceptionBinaryOperator<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