How to map an OptionalLong to an Optional?

前端 未结 4 1962
逝去的感伤
逝去的感伤 2021-02-20 01:59

I have an instance of OptionalLong. But one of my libraries requires an Optional as a parameter.

How can I convert my Option

4条回答
  •  [愿得一人]
    2021-02-20 02:33

    One more possibility, though only from JDK 9 is via the new OptionalLong.stream() method, which returns a LongStream. This can then be boxed to a Stream:

    OptionalLong optionalLong = OptionalLong.of(32);
    Optional optional = optionalLong.stream().boxed().findFirst();
    

    With JDK 8 something similar can be done, by stepping out to the Streams utility class in Guava:

    Optional optional = Streams.stream(optionalLong).boxed().findFirst();
    

提交回复
热议问题