scala and java data type conversion

When java and scala are used together, sometimes the mutual conversion of data types is involved. For example, when using scala json4s, java data types are not supported in many places.

Convert java data type to scala data type

import scala.collection.JavaConverters._
var json = JObject()
val links = value.getLink_id_vec.asScala.map { x => x.toLong }.toList
json = json.~("link_id_vec" -> links)

Convert scala data type to java data type

import scala.collection.JavaConverters._

val l = List(1L,2L,3L,4L)

l.map(java.lang.Long.valueOf).asJava
// or 
l.map(_.asInstanceOf[AnyRef]).asJava
// or
l.map(Long.box).asJava

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324982123&siteId=291194637