How to map an OptionalLong to an Optional?

前端 未结 4 1896
逝去的感伤
逝去的感伤 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:34

    I don't know simpler solutions but this will do what you need.

    OptionalLong secondScreenHeight = OptionalLong.of(32l);
    Optional optional = secondScreenHeight.isPresent() 
        ? Optional.of(secondSceenHeight.getAsLong()) 
        : Optional.empty();
    api.setHeight(optional);
    

提交回复
热议问题