英文:
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 aIterable[SomeJavaCollectionType]
flatten
works only ifSomeJavaCollectionType
can be "transformers/"viewed" as aIterable[SomeOtherType]
so thatIterable[SomeJavaCollectionType]
can be viewed asIterable[Iterable[SomeOtherType]]
and thus flattened- there's no such "implicit view" available for your actual
SomeJavaCollectionType
which seems to beJavaMap[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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论