Convert Scala Option to Java Optional

前端 未结 7 1264
孤街浪徒
孤街浪徒 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条回答
  •  猫巷女王i
    2021-02-12 16:04

    There is an asJava solution now! It's available from 2.10.

    Example:

    import scala.compat.java8.OptionConverters._
    
    class Test {
      val o = Option(2.7)
      val oj = o.asJava        // Optional[Double]
      val ojd = o.asPrimitive  // OptionalDouble
      val ojds = ojd.asScala   // Option(2.7) again
    }
    

    More details here.

提交回复
热议问题