Class SingletonCollectors

java.lang.Object
io.blt.util.stream.SingletonCollectors

public final class SingletonCollectors extends Object
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.

  • Method Details

    • toOptional

      Returns a Collector that accumulates the only element, if any, into an Optional.
      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector that accumulates the only element, if any, into an Optional.
      Throws:
      IllegalArgumentException - if more than one element is present
    • toNullable

      Returns a Collector that accumulates the only element, if any, into a nullable Object.
      Type Parameters:
      T - the type of the input elements
      Returns:
      a Collector that accumulates the only element, if any, into a nullable Object.
      Throws:
      IllegalArgumentException - if more than one element is present