### Question:
OptionalCollectors { public static Collector<Long, ?, OptionalLong> toOptionalLong() { return toOptional(OptionalLong::empty, OptionalLong::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 toOptionalLong() { Collector<Long, ?, OptionalLong> collector = OptionalCollectors.toOptionalLong(); assertThat(Stream.<Long>empty().collect(collector)).isEmpty(); assertThat(Stream.of((Long) null).collect(collector)).isEmpty(); assertThat(Stream.of(1L).collect(collector)).hasValue(1L); assertThatThrownBy( () -> Stream.of(1L, 2L).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: ['1', '2', ...]"); assertThatThrownBy( () -> Stream.of(1L, null).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: ['1', null, ...]"); assertThatThrownBy( () -> Stream.of(null, 2L).collect(collector)) .isInstanceOf(IllegalStateException.class) .hasMessage("Multiple values for optional: [null, '2', ...]"); }