Scala 2.13 error: type mismatch found Iterable[String,Double] required: java.util.Map[String,Double]

huangapple go评论65阅读模式
英文:

Scala 2.13 error: type mismatch found Iterable[String,Double] required: java.util.Map[String,Double]

问题

我有一个Scala项目,需要将它从Scala 2.12迁移到2.13版本。我有一段简单的代码,用于将Scala的Map转换为Java的java.util.Map。我使用了asJava方法来实现,代码如下:

val jMap : java.util.Map[String,java.lang.Double]= imputerMap.mapValues(Double.box).asJava

但我遇到了一个错误:

found   : Iterable[(String, Double)]
[ERROR]  required: java.util.Map[String,Double]
[ERROR]     val jMap : java.util.Map[String,java.lang.Double]= imputerMap.mapValues(Double.box).asJava

scala 2.13的文档中,我看到导入方式已经改成了:

import scala.jdk.CollectionConverters._

但我不明白为什么这会导致错误。现在我该如何进行转换?

英文:

I have a scala project that I need to migrate from scala 2.12 to 2.13. I have a simple code that converts a scala Map to a Java.util.Map. I am using the asJava method to do so, the code is simple

val jMap : java.util.Map[String,java.lang.Double]= imputerMap.mapValues(Double.box).asJava

but I get an error

found   : Iterable[(String, Double)]
[ERROR]  required: java.util.Map[String,Double]
[ERROR]     val jMap : java.util.Map[String,java.lang.Double]= imputerMap.mapValues(Double.box).asJava

At the documentation of scala 2.13 I see that the imported changed to
import scala.jdk.CollectionConverters._
but I dont see that it should break
How can I do the conversion now?

答案1

得分: 2

如您所见,从Scala 2.12到Scala 2.13,Scala和Java集合之间的转换发生了变化:

要启用这些转换,只需从JavaConverters对象导入它们:

import collection.JavaConverters._

要启用这些转换,从CollectionConverters对象导入它们:

import scala.jdk.CollectionConverters._

此外,Map类中mapValues方法的签名已更改:

/** 通过对每个检索到的值应用函数来转换此映射。
 *  @param  f   用于转换此映射值的函数。
 *  @return 将此映射的每个键映射到`f(this(key))`的映射视图。生成的映射包装原始映射,不复制任何元素。
 */
override def mapValues[W](f: V => W): Map[K, W]
/** 通过对每个检索到的值应用函数来转换此映射。
 *  @param  f   用于转换此映射值的函数。
 *  @return 将此映射的每个键映射到`f(this(key))`的映射视图。生成的映射包装原始映射,不复制任何元素。
 */
@deprecated("使用 .view.mapValues(f)。将来的版本将包括此方法的严格版本(目前为 .view.mapValues(f).toMap)。", "2.13.0")
def mapValues[W](f: V => W): MapView[K, W]

mapValues返回一个MapView。您可以在以下链接中找到有关视图的信息:

要从集合转到其视图,您可以在集合上使用view方法。如果xs是某个集合,那么xs.view是相同的集合,但所有转换器都以延迟方式实现。要从视图返回到严格集合,您可以使用带有严格集合工厂参数的转换操作(例如,xs.view.to(List))。

因此,您的代码应该重构为:

import scala.jdk.CollectionConverters._

val jMap: java.util.Map[String, java.lang.Double] = 
  imputerMap
    .view // 使用视图,如弃用的方法建议
    .mapValues(Double.box)
    .to(Map) // 从延迟集合转换为严格集合
    .asJava  // 转换为Java集合的方法
英文:

As you already saw, the convertions between scala and java collections have changed from scala 2.12 to scala 2.13:

> To enable these conversions, simply import them from the JavaConverters object:
> scala
> import collection.JavaConverters._
>

> To enable these conversions, import them from the CollectionConverters object:
> scala
> import scala.jdk.CollectionConverters._
>


Also the signature of the method mapValues from Map class changed:

  /** Transforms this map by applying a function to every retrieved value.
   *  @param  f   the function used to transform values of this map.
   *  @return a map view which maps every key of this map
   *          to `f(this(key))`. The resulting map wraps the original map without copying any elements.
   */
  override def mapValues[W](f: V => W): Map[K, W]
  /** Transforms this map by applying a function to every retrieved value.
    *  @param  f   the function used to transform values of this map.
    *  @return a map view which maps every key of this map
    *          to `f(this(key))`. The resulting map wraps the original map without copying any elements.
    */
  @deprecated("Use .view.mapValues(f). A future version will include a strict version of this method (for now, .view.mapValues(f).toMap).", "2.13.0")
  def mapValues[W](f: V => W): MapView[K, W]

mapValues returns a MapView. You can find info about views in the following links

> To go from a collection to its view, you can use the view method on the collection. If xs is some collection, then xs.view is the same collection, but with all transformers implemented lazily. To get back from a view to a strict collection, you can use the to conversion operation with a strict collection factory as parameter (e.g. xs.view.to(List))

So, your code should be refactored as

import scala.jdk.CollectionConverters._

val jMap: java.util.Map[String, java.lang.Double] = 
  imputerMap
    .view // use view as the deprecated method suggeted
    .mapValues(Double.box)
    .to(Map) // transform from lazy collection to strict one
    .asJava  // the method to convert to java collection

huangapple
  • 本文由 发表于 2023年7月20日 11:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定