Scala Java 错误:值过滤器不是 java.util.Map 的成员。课外作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24960835/
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
Scala Java Error: value filter is not a member of java.util.Map. Works outside of class
提问by dlite922
I'm trying to refactor some Scala code in Eclipse and run into this compilation error:
我试图在 Eclipse 中重构一些 Scala 代码并遇到这个编译错误:
value filter is not a member of java.util.Map
值过滤器不是 java.util.Map 的成员
import java.io.File
import com.typesafe.config._
class ConfigLoader {
def parseFile( confFile : File) {
val conf = ConfigFactory.parseFile(confFile).root().unwrapped();
for((k,v) <- conf; (dk, dv) = (k, v.toString())) config.param += (dk -> dv);
}
(config is an object with "param" being a Map of String:String)
(config 是一个对象,其中“param”是 String:String 的 Map)
This code was pull exactly from Main() where it worked fine like so:
这段代码正是从 Main() 中提取的,它运行良好,如下所示:
object Main extends Logging {
def main(args: Array[String]) {
//code cropped for readability.
//config.param["properties"] is absolute path to a passed-in properties file.
val conf = ConfigFactory.parseFile(new java.io.File(config.param("properties"))).root().unwrapped();
for((k,v) <- conf; (dk, dv) = (k, v.toString())) config.param+=(dk -> dv);
as you can see the code is exactly the same. I've imported the same libraries. All i'm doing different in main now is instantiating ConfigLoader
and calling it like so:
如您所见,代码完全相同。我已经导入了相同的库。我现在在 main 中所做的不同是实例化ConfigLoader
并像这样调用它:
cfgLoader.parseFile(config.param("properties"))
cfgLoader.parseFile(config.param("properties"))
Any ideas what's causing the error just by moving it to a class?
任何想法只是将其移动到一个类中导致错误的原因是什么?
I've googled the error and it seems to be pretty generic.
我用谷歌搜索了错误,它似乎非常通用。
采纳答案by dlite922
Turns out I was missing a tricky import after all:
事实证明,我毕竟错过了一个棘手的导入:
import collection.JavaConversions._
import collection.JavaConversions._
not to be confused with JavaConverters._
which I did have.
不要与JavaConverters._
我确实拥有的混淆。
Hope this helps someone else.
希望这对其他人有帮助。
回答by Juh_
The problem is that you are using a java Map which doesn't implement the monad api (map, flatMap, ...) required to use scala for-comprehension.
问题是您使用的 java Map 没有实现使用 scala for-comprehension 所需的 monad api(map、flatMap 等)。
More specifically, in your example, the compiler is complaining about missing .filter
method. This is because you unpack each item of the map: (key, value) <- monad
instead of a direct assignment, such as entry <- monad
. But even if you did use direct assignment, it would complain about missing .map
or .flatMap
. See this answer to "how to make you own for-comprehension compliant scala monadfor details on the required api.
更具体地说,在您的示例中,编译器抱怨缺少.filter
方法。这是因为您解压了地图的每个项目:(key, value) <- monad
而不是直接赋值,例如entry <- monad
. 但即使你确实使用了直接赋值,它也会抱怨缺少.map
或.flatMap
. 有关所需 api 的详细信息,请参阅“如何让您拥有符合理解性的 scala monad”的答案。
The simplest solution to your problem is to convert you java map into scala map using:
解决您的问题的最简单方法是使用以下方法将您的 java 地图转换为 Scala 地图:
import scala.collection.JavaConverters._
...
for((k,v) <- conf.asScala) ...
The import includes implicit that add the .asScala
method to java map
导入包含将.asScala
方法添加到 java map 的隐式