Convert Scala Option to Java Optional

前端 未结 7 1249
孤街浪徒
孤街浪徒 2021-02-12 15:39

I need to convert Scala Option to Java Optional. I managed to wrote this:

public  Optional convertOption2Optional(Option option) {
           


        
7条回答
  •  悲哀的现实
    2021-02-12 15:54

    For a given input parameter opt: Option[T] of some class, you can define a method inside that class like the following:

    def toOptional(implicit ev: Null <:< T): Optional[T] = Optional.ofNullable(opt.orNull).

提交回复
热议问题