简体   繁体   English

将scala.List [scala.Long]转换为List <java.util.Long>

[英]Convert scala.List[scala.Long] to List<java.util.Long>

In my Scala code I need to interact with Java libraries. 在我的Scala代码中,我需要与Java库进行交互。

The method I want to use expects parameter of type List<java.util.Long> , but I have Scala type Option[List[Long]] . 我想要使​​用的方法需要List<java.util.Long>类型的参数,但我有Scala类型Option[List[Long]] The problem I faced is that I need manually apply Predef.long2Long function to convert from Scala Long to java.util.Long : 我遇到的问题是我需要手动应用Predef.long2Long函数将Scala Long转换为java.util.Long

val userIds = Option(List(1,2,3,4))
val methodParameter = userIds.map(list => list.map(long2Long).asJava).orNull

is there some way to convert this list in more elegant and concise way? 有没有办法以更优雅和简洁的方式转换此列表?

EDIT: I can even simplify question: how to convert Option[scala.Boolean] to java.util.Boolean ? 编辑:我甚至可以简化问题:如何将Option[scala.Boolean]转换为java.util.Boolean Here I need to do same: 在这里,我需要做同样的事情:

isUsed.map(isUsedVal => boolean2Boolean(isUsedVal)).orNull,

For the List[Long] : 对于List[Long]

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

For the Option[Boolean] : 对于Option[Boolean]

val mb = Some(true)

mb.map(java.lang.Boolean.valueOf).orNull
// or 
mb.map(_.asInstanceOf[AnyRef]).orNull
// or 
mb.map(Boolean.box).orNull

Can you use JavaConverters ? 你可以使用JavaConverters吗? and do something like that 并做那样的事情

val sl = new scala.collection.mutable.ListBuffer[Int]
val jl : java.util.List[Int] = sl.asJava

It's worth mentioning that long2Long and boolean2Boolean are implicit, but that doesn't help you much to make this more elegant or clear, I think. 值得一提的是long2Longboolean2Boolean是隐式的,但我认为这对你说这更优雅或更清楚没有多大帮助。 It does allow you to things like: 它确实允许你这样的事情:

type JB = java.lang.Boolean
isUsed.fold(null : JB)(implicitly)
//Or:
isUsed.map { b => b : JB }.orNull
//Or even:
isUsed.map[JB](identity).orNull

But that's not particularly readable, in my opinion. 但在我看来,这并不是特别易读。

These sorts of methods are perfect candidates for an implicit class method . 这些类型的方法是隐式类方法的完美候选。 You can enrich your classes with these methods so that you only have to write the tedious or inelegant code once. 您可以使用这些方法丰富您的类,这样您只需编写一次繁琐或不优雅的代码。 For example, you might consider: 例如,您可以考虑:

object MoreJavaConversions {
    import scala.collection.JavaConverters._
    implicit class RichListLong(val list: Option[List[Long]]) extends AnyVal {
        def asJava : java.util.List[java.lang.Long] = list.map(_.map(long2Long).asJava).orNull
    }
    implicit class RichOptionBoolean(val bool: Option[Boolean]) extends AnyVal {
        def asJava : java.lang.Boolean = bool.map(boolean2Boolean).orNull
    }
}

Which would later allow you to do: 以后允许你这样做:

import MoreJavaConversions._
val myUsers = Option(List(1l, 2l, 3l))

myUsers.asJava //java.util.List[java.lang.Long]
Option(true).asJava //java.lang.Boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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