Class OptionalCollectors


  • public class OptionalCollectors
    extends java.lang.Object
    Factory methods for collectors of optional types.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private OptionalCollectors()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.stream.Collector<T,​?,​java.util.Optional<T>> toOptional()
      Returns a Collector that accumulates 0 or 1 input elements into an Optional<T>.
      static <T,​O>
      java.util.stream.Collector<T,​?,​O>
      toOptional​(java.util.function.Supplier<O> empty, java.util.function.Function<T,​O> factory)
      Returns a Collector that accumulates 0 or 1 input elements into an arbitrary optional-style container type.
      static java.util.stream.Collector<java.lang.Double,​?,​java.util.OptionalDouble> toOptionalDouble()
      Returns a Collector that accumulates 0 or 1 input Double elements into an OptionalDouble.
      static java.util.stream.Collector<java.lang.Integer,​?,​java.util.OptionalInt> toOptionalInt()
      Returns a Collector that accumulates 0 or 1 input Integer elements into an OptionalInt.
      static java.util.stream.Collector<java.lang.Long,​?,​java.util.OptionalLong> toOptionalLong()
      Returns a Collector that accumulates 0 or 1 input Long elements into an OptionalLong.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OptionalCollectors

        private OptionalCollectors()
    • Method Detail

      • toOptional

        public static <T> java.util.stream.Collector<T,​?,​java.util.Optional<T>> toOptional()
        Returns a Collector that accumulates 0 or 1 input elements into an Optional<T>. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to Optional.empty().
        Type Parameters:
        T - the collected type
        Returns:
        a Collector which collects 0 or 1 input elements into an Optional<T>.
      • toOptionalInt

        public static java.util.stream.Collector<java.lang.Integer,​?,​java.util.OptionalInt> toOptionalInt()
        Returns a Collector that accumulates 0 or 1 input Integer elements into an OptionalInt. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to OptionalInt.empty().
        Returns:
        a Collector which collects 0 or 1 input Integer elements into an OptionalInt.
      • toOptionalLong

        public static java.util.stream.Collector<java.lang.Long,​?,​java.util.OptionalLong> toOptionalLong()
        Returns a Collector that accumulates 0 or 1 input Long elements into an OptionalLong. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to OptionalLong.empty().
        Returns:
        a Collector which collects 0 or 1 input Long elements into an OptionalLong.
      • toOptionalDouble

        public static java.util.stream.Collector<java.lang.Double,​?,​java.util.OptionalDouble> toOptionalDouble()
        Returns a Collector that accumulates 0 or 1 input Double elements into an OptionalDouble. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to OptionalDouble.empty().
        Returns:
        a Collector which collects 0 or 1 input Double elements into an OptionalDouble.
      • toOptional

        public static <T,​O> java.util.stream.Collector<T,​?,​O> toOptional​(java.util.function.Supplier<O> empty,
                                                                                           java.util.function.Function<T,​O> factory)
        Returns a Collector that accumulates 0 or 1 input elements into an arbitrary optional-style container type. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to an empty container.
        Type Parameters:
        T - The optional element type.
        O - The optional type, which may incorporate the T generic parameter e.g. Optional<T>.
        Parameters:
        empty - Supplies an instance of the optional type with no value.
        factory - Returns an instance of the optional type with the input parameter as the value.
        Returns:
        a Collector which collects 0 or 1 input elements into an arbitrary optional-style container type.