简体   繁体   English

有没有更好的方法将Scala Map转换为Java Map?

[英]Is there a better way to convert a Scala Map to Java Map?

I found a solution, but it is not too elegant: 我找到了一个解决方案,但它不是太优雅:

mapAsJavaMap(Map("param1"->someObject, "param2"->otherObject ))

I found some examples containing "asJava" calls, but it is not working in my configuration of Scala 2.10.X. 我找到了一些包含“ asJava”调用的示例,但是在我的Scala 2.10.X的配置中它不起作用。

There is object JavaConverters that will do the trick for you: 有对象JavaConverters可以为您解决问题:

import scala.collection.JavaConverters.mapAsJavaMapConverter

Map("param1"-> 1, "param2"-> 2).asJava
//> res0: java.util.Map[String,Int] = {param1=1, param2=2}

Edit: It is indeed a bad programming habbit to use wildcard imports as they will clutter your namespace and they might introduce accidental implicit conversion/values into the scope. 编辑:使用通配符导入确实是一个不好的编程习惯,因为它们会使您的名称空间混乱,并且可能将意外的隐式转换/值引入范围。 So changed wildcard import to explicit import. 因此将通配符导入更改为显式导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM