【发布时间】:2017-07-12 14:15:01
【问题描述】:
我有一个例外:
java.lang.ClassCastException: scala.collection.immutable.Map$ 不能 被强制转换为 scala.collection.immutable.Map
我在这部分代码中得到的:
val iterator = new CsvMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readerFor(Map.getClass).`with`(CsvSchema.emptySchema().withHeader()).readValues(reader)
while (iterator.hasNext) {
println(iterator.next.asInstanceOf[Map[String, String]])
}
那么,有什么办法可以避免这个问题,因为:
val iterator = new CsvMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readerFor(Map[String,String].getClass).`with`(CsvSchema.emptySchema().withHeader()).readValues(reader)
没用,因为我明白了
[error] Unapplied methods are only converted to functions when a function type is expected.
[error] You can make this conversion explicit by writing `apply _` or `apply(_)` instead of `apply`.
提前致谢
【问题讨论】:
-
Map.getClass是 Map 伴随对象的类。你可能想要classOf[Map[_, _]] -
$符号后面没有什么吗?只是因为Java中的$把内部类和主类分开了…… -
@UsagiMiyamoto :scala 编译器只是在
objects 的类名末尾添加一个美元符号(因此object MyObject被编译为名为MyObject$的类)。跨度> -
@OlegPyzhcov 谢谢,我忘了 classOf 函数。现在我收到
Can not construct instance of scala.collection.immutable.Map: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information -
上一个错误
Can not construct instance of scala.collection.immutable.Map: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information已通过使用classOf[java.util.Map[_,_]]解决。