How to map an OptionalLong to an Optional?

前端 未结 4 1972
逝去的感伤
逝去的感伤 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条回答
  •  旧时难觅i
    2021-02-20 02:19

    You could do this:

    final OptionalLong optionalLong = OptionalLong.of(5);
    
    final Optional optional = Optional.of(optionalLong)
                .filter(OptionalLong::isPresent)
                .map(OptionalLong::getAsLong);
    

提交回复
热议问题