简体   繁体   中英

How to convert Scala's List[Double] to java.util.List[java.lang.Double]?

I have only seen examples where the result is a Java list of Scala doubles. I got as far as

def getDistance(): java.util.List[java.lang.Double] = {
  val javadistance = distance.toList.asJava
  javadistance
}

but this is still a Java list containing Scala doubles ( distance is a member of the same class as getDistance ).

One has to use the java boxed variant in a map:

  def getDistance(): java.util.List[java.lang.Double] = {
    distance.toList.map(Double.box).asJava
  }

Other than Scala 2.13+ box method, you can use:

def getDistance(): java.util.List[java.lang.Double] = {
  val javadistance = distance.toList.map(java.lang.Double.valueOf).asJava
  javadistance
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM