Is there a way in Scala
to convert a List[Int]
to java.util.List[java.lang.Integer]
?
I'm interfacing with Java (Thrift).
JavaConversions
supports List --> java.util.List
, and implicits exist between Int --> java.lang.Integer
, but from what I can tell I would still need an extra pass to manually do the conversion:
val y = List(1) val z: java.util.List[Integer] = asList(y) map { (x: Int) => x : java.lang.Integer }
Apparently you need both conversions. However, you can group them in a single implicit conversion:
implicit def toIntegerList( lst: List[Int] ) = seqAsJavaList( lst.map( i => i:java.lang.Integer ) )
Example:
scala> def sizeOf( lst: java.util.List[java.lang.Integer] ) = lst.size scala> sizeOf( List(1,2,3) ) res5: Int = 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With