Scala 2.13 No implicit view available from java.util.Map[String,Double] => scala.collection.IterableOnce[B]

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

Scala 2.13 No implicit view available from java.util.Map[String,Double] => scala.collection.IterableOnce[B]

问题

The error you're encountering is related to an implicit conversion issue in your Scala code when migrating from Scala 2.12 to Scala 2.13. The error message specifically mentions that there is no implicit view available from java.util.Map[String, Double] => scala.collection.IterableOnce[B].

In Scala 2.13, there have been changes to how collections work, including the introduction of scala.collection.IterableOnce, which is a trait that represents collections that can be converted into other collections. The error you're seeing is because the flatten.toMap.asJava sequence of operations is not able to automatically convert a java.util.Map[String, Double] into a compatible scala.collection.IterableOnce[B].

To fix this error, you may need to explicitly convert your java.util.Map into a Scala collection (e.g., a scala.collection.mutable.Map) before applying the flatten.toMap.asJava operations. You can do this by using the .asScala method provided by the Java to Scala collection conversion:

Here's how you can modify your code:

import scala.collection.JavaConverters._

def getMetrics(): java.util.Map[String, Double] = {
    transformers.collect {
        case t: EvaluationTransformFunction => t.getMetric.asScala
    }.flatten.toMap.asJava
}

By using .asScala, you convert your Java Map to a Scala Map, making it compatible with the subsequent operations, and then convert it back to a Java Map with .asJava if necessary. This should resolve the implicit conversion issue and eliminate the error.

英文:

I migrate some scala code from 2.12 to 2.13 and I have the following code

def getMetrics(): java.util.Map[String, Double] ={
    transformers.map{
      case transformer => transformer match{
        case t: EvaluationTransformFunction => t.getMetric.asJava
      }
    }.flatten.toMap.asJava
  }

that produces the error

error: No implicit view available from java.util.Map[String,Double] => scala.collection.IterableOnce[B].
[ERROR]     }.flatten.toMap.asJava
[ERROR]       ^

can you explain what is the error about?

答案1

得分: 2

以下是翻译好的部分:

  • transformers.map(...) 的类型是 Iterable[SomeJavaCollectionType]
  • flatten 只在 SomeJavaCollectionType 可以被视为 Iterable[SomeOtherType] 时才起作用,使得 Iterable[SomeJavaCollectionType] 可以被视为 Iterable[Iterable[SomeOtherType]],从而被展开
  • 对于你实际的 SomeJavaCollectionType,似乎没有可用的 "implicit view",它似乎是 JavaMap[String, Double]

我对在Scala 2.12中能够工作感到有些惊讶。也许在Scala 2.13中有一个 import 可以使其工作(即将 "implicit view" 引入范围内)。

无论如何,我建议只在方法的最后将数据转换为Java集合,去掉内部的 asJava

def getMetrics(): java.util.Map[String, Double] = {
    transformers.map {
        case transformer => transformer match {
            case t: EvaluationTransformFunction => t.getMetric
        }
    }.flatten.toMap.asJava
}
英文:

We're missing a bit more info about the exact types you are manipulating but the idea is that:

  • the type of transformers.map(...) is a Iterable[SomeJavaCollectionType]
  • flatten works only if SomeJavaCollectionType can be "transformers/"viewed" as a Iterable[SomeOtherType] so that Iterable[SomeJavaCollectionType] can be viewed as Iterable[Iterable[SomeOtherType]] and thus flattened
  • there's no such "implicit view" available for your actual SomeJavaCollectionType which seems to be JavaMap[String, Double] if I read it correctly

I'm slightly surprised it was working in Scala 2.12. Maybe there's an import that would make it work in Scala 2.13 as well (i.e. bring the "implicit view" in scope).

Anyway, I would just get rid of the inner asJava and only convert to Java collection at the end of your method:

def getMetrics(): java.util.Map[String, Double] ={
    transformers.map{
      case transformer => transformer match{
        case t: EvaluationTransformFunction => t.getMetric
      }
    }.flatten.toMap.asJava
  }

huangapple
  • 本文由 发表于 2023年8月10日 10:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872372.html
匿名

发表评论

匿名网友

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

确定