Package io.blt.util.stream
Class SingletonCollectors
java.lang.Object
io.blt.util.stream.SingletonCollectors
Implementations of
Collector that reduce to exactly one or zero elements.
e.g.,
var maybeActiveStep = sequentialSteps.stream()
.filter(SequentialStep::isActive)
.collect(SingletonCollectors.toOptional());
If more than one element is present, then IllegalArgumentException is thrown.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final class -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> SingletonCollectors.SingletonCollector<T, T> Returns aCollectorthat accumulates the only element, if any, into a nullableObject.static <T> SingletonCollectors.SingletonCollector<T, Optional<T>> Returns aCollectorthat accumulates the only element, if any, into anOptional.
-
Method Details
-
toOptional
Returns aCollectorthat accumulates the only element, if any, into anOptional.- Type Parameters:
T- the type of the input elements- Returns:
- a
Collectorthat accumulates the only element, if any, into anOptional. - Throws:
IllegalArgumentException- if more than one element is present
-
toNullable
Returns aCollectorthat accumulates the only element, if any, into a nullableObject.- Type Parameters:
T- the type of the input elements- Returns:
- a
Collectorthat accumulates the only element, if any, into a nullableObject. - Throws:
IllegalArgumentException- if more than one element is present
-