### Question:
OptionalCollectors { public static Collector<Double, ?, OptionalDouble> toOptionalDouble() { return toOptional(OptionalDouble::empty, OptionalDouble::of); } private  OptionalCollectors(); static Collector<T, ?, Optional<T>> toOptional(); static Collector<Integer, ?, OptionalInt> toOptionalInt(); static Collector<Long, ?, OptionalLong> toOptionalLong(); static Collector<Double, ?, OptionalDouble> toOptionalDouble(); static Collector<T, ?, O> toOptional(Supplier<O> empty,
                                                     Function<T, O> factory);  }### Answer:
@Test public void toOptionalDouble() { Collector<Double, ?, OptionalDouble> collector = OptionalCollectors.toOptionalDouble(); assertThat(Stream.<Double>empty().collect(collector)).isEmpty(); assertThat(Stream.of((Double) null).collect(collector)).isEmpty(); assertThat(Stream.of(1.5d).collect(collector)).hasValue(1.5d); assertThatThrownBy( () -> Stream.of(1.5d, 2.25d).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: ['1.5', '2.25', ...]"); assertThatThrownBy( () -> Stream.of(1.5d, null).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: ['1.5', null, ...]"); assertThatThrownBy( () -> Stream.of(null, 2.25d).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: [null, '2.25', ...]"); }