Package com.github.bannmann.labs.core
Class StreamExtras
- java.lang.Object
-
- com.github.bannmann.labs.core.StreamExtras
-
public final class StreamExtras extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> BinaryOperator<T>atMostOne()Use withStream.reduce(BinaryOperator)to ensure there is at most one element.static <T,E extends RuntimeException>
BinaryOperator<T>atMostOneThrowing(Supplier<E> exception)Use withStream.reduce(BinaryOperator)to ensure there is at most one element.
-
-
-
Method Detail
-
atMostOne
public static <T> BinaryOperator<T> atMostOne()
Use withStream.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'sMoreCollectors.onlyElement().- Type Parameters:
T- element type- Returns:
- an operator to use with
Stream.reduce(BinaryOperator)
-
atMostOneThrowing
public static <T,E extends RuntimeException> BinaryOperator<T> atMostOneThrowing(Supplier<E> exception)
Use withStream.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'sMoreCollectors.onlyElement().- Type Parameters:
T- element typeE- type of the exception to throw if more than one element is encountered- Parameters:
exception- thrown if more than one element is encountered
-
-