Scala 将 List[Int] 转换为 java.util.List[java.lang.Integer]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6991340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:19:47  来源:igfitidea点击:

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

scalascala-java-interop

提问by Pandora Lee

Is there a way in Scalato convert a List[Int]to java.util.List[java.lang.Integer]?

有没有办法Scala将 a 转换List[Int]java.util.List[java.lang.Integer]

I'm interfacing with Java (Thrift).

我正在与 Java (Thrift) 交互。

JavaConversionssupports 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:

JavaConversions支持List --> java.util.List和 之间存在隐式Int --> java.lang.Integer,但据我所知,我仍然需要额外的传递来手动进行转换:

val y = List(1)     
val z: java.util.List[Integer] = asList(y)  map { (x: Int) => x : java.lang.Integer }

采纳答案by paradigmatic

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

回答by huynhjl

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.

因为 Int 的底层表示是 Integer 您可以直接转换为java.util.List[java.lang.Integer]. 它将为您节省O(n)操作和一些隐含的东西。

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:

然后你可以像这样从 Java 中使用:

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 seqAsJavaListinstead)

请注意,在 2.9.0 上,我收到了一个弃用警告asListseqAsJavaList改为使用)

回答by user unknown

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?

我不确定,这里是否需要转换 Int=>Integer 或 Int=>int。你能试试吗?

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]

回答by jm0

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:

由于从 Int 到 Integer 的隐式转换是标准库的一部分,因此在这种情况下的用法将如下所示:

  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)) }

回答by ElBaulP

I was trying to pass a Map[String, Double]to a Java method. But the problem was JavaConversionsconverts the Mapto a java Map, but leaves the scala Doubleas is, instead of converting it to java.lang.Double. After a few hours of seaching I found [Alvaro Carrasco's answer])https://stackoverflow.com/a/40683561/1612432), it is as simple as doing:

我试图将 a 传递Map[String, Double]给 Java 方法。但问题是JavaConversions将 the 转换Map为 java Map,但将 scala 保留Double原样,而不是将其转换为java.lang.Double. 经过几个小时的搜索,我找到了 [Alvaro Carrasco 的答案]) https://stackoverflow.com/a/40683561/1612432),它就像做一样简单:

val scalaMap = // Some Map[String, Double]
val javaMap = scalaMap.mapValues(Double.box)

After this, javaMapis a Map[String, java.lang.Double]. Then you can pass this to a java function that expects a Map<String, Double>and thanks to implicit conversions the Scala Map will be converted to java.util.Map

在此之后,javaMap是一个Map[String, java.lang.Double]. 然后你可以将它传递给一个需要 a 的 java 函数,Map<String, Double>并且由于隐式转换,Scala Map 将被转换为java.util.Map

In your case would be the same, but with Int.box:

在你的情况下是一样的,但有Int.box

val y = List(1)
val javay = y.map(Int.box)

回答by Xavier Guihot

Starting Scala 2.13, the standard library includes scala.jdk.CollectionConverterswhich provides Scala to Java implicit collection conversions.

开始Scala 2.13,标准库包括scala.jdk.CollectionConverters提供 Scala 到 Java 隐式集合转换的内容。

Which we can combine with java.lang.Integer::valueOfto convert Scala's Intto Java's Integer:

我们可以结合使用java.lang.Integer::valueOf将 Scala 转换Int为 Java 的Integer

import scala.jdk.CollectionConverters._

List(1, 2, 3).map(Integer.valueOf).asJava
// java.util.List[Integer] = [1, 2, 3]