Scala convert List[Int] to a java.util.List[java.lang.Integer]

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

Is there a good way in Scala to convert between a List[Int] to a 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 } 

回答1:

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 


回答2:

Because the underlying representation of Int is Integer you can cast directly to java.util.List[java.lang.Integer]. It will save you an O(n) operation and some implicit stuff.

import collection.JavaConversions._  class A {   def l() = asList(List(1,2)).asInstanceOf[java.util.List[java.lang.Integer]] } 

Then you can use from Java like this:

A a = new A(); java.util.List<Integer> l = a.l(); 

Note that on 2.9.0 ,I get a deprecation warning on asList (use seqAsJavaList instead)



回答3:

Did you try:

val javalist = collection.JavaConversions.asJavaList (y) 

I'm not sure, whether you need a conversion Int=>Integer or Int=>int here. Can you try it out?

Update: The times, they are a changing. Today you'll get a deprecated warning for that code. Use instead:

import scala.collection.JavaConverters._ val y = List (1) > y: List[Int] = List(1)  val javalist = (y).asJava > javalist: java.util.List[Int] = [1] 


回答4:

This doesn't have an implicit at the outmost layer, but I like this generic approach and have implemented it for a couple of types of collections (List, Map).

import java.util.{List => JList} import scala.collection.JavaConverters._    def scalaList2JavaList[A, B](scalaList: List[A])                               (implicit a2bConversion: A => B): JList[B] =     (scalaList map a2bConversion).asJava 

Since an implicit conversion from Int to Integer is part of standard lib, usage in this case would just look like this:

  scalaList2JavaList[Int, Integer](someScalaList) 

In the other direction!

(since I have these available anyway as they were my original implementations...)

import java.util.{List => JList} import scala.collection.JavaConversions._    def javaList2ScalaList[A, B](javaList: JList[A])                               (implicit a2bConversion: A => B): List[B] =     javaList.toList map a2bConversion 

Usage:

  javaList2ScalaList[Integer, Int](someJavaList) 

This can then be re-used for all lists so long as an implicit conversion of the contained type is in scope.

(And in case you're curious, here is my implementation for map...)

  def javaMap2ScalaMap[A, B, C, D](javaMap: util.Map[A, B])(implicit a2cConversion: A => C, b2dConversion: B => D): Map[C, D] =     javaMap.toMap map { case (a, b) => (a2cConversion(a), b2dConversion(b)) } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!