从 Java 属性获取 Scala 映射
我试图使用 java 迭代器和/或枚举将环境变量拉入 scala 脚本中,并意识到弗兰肯斯坦博士可能声称出身,所以我从丑陋的树中破解了以下内容:
import java.util.Map.Entry
import System._
val propSet = getProperties().entrySet().toArray()
val props = (0 until propSet.size).foldLeft(Map[String, String]()){(m, i) =>
val e = propSet(i).asInstanceOf[Entry[String, String]]
m + (e.getKey() -> e.getValue())
}
例如打印上述相同的环境
props.keySet.toList.sortWith(_ < _).foreach{k =>
println(k+(" " * (30 - k.length))+" = "+props(k))
}
,请不要这样做不用着手完善这个 t$#d,只需向我展示我确信在这种情况下存在的 scala gem(即 java Properties --> scala.Map),提前感谢;@)
I was trying to pull environment variables into a scala script using java Iterators and / or Enumerations and realised that Dr Frankenstein might claim parentage, so I hacked the following from the ugly tree instead:
import java.util.Map.Entry
import System._
val propSet = getProperties().entrySet().toArray()
val props = (0 until propSet.size).foldLeft(Map[String, String]()){(m, i) =>
val e = propSet(i).asInstanceOf[Entry[String, String]]
m + (e.getKey() -> e.getValue())
}
For example to print the said same environment
props.keySet.toList.sortWith(_ < _).foreach{k =>
println(k+(" " * (30 - k.length))+" = "+props(k))
}
Please, please don't set about polishing this t$#d, just show me the scala gem that I'm convinced exists for this situation (i.e java Properties --> scala.Map), thanks in advance ;@)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
斯卡拉2.10.3
Scala 2.10.3
Scala 2.7:
尽管这需要一些类型转换。让我再做一点工作。
好吧,这很容易。现在让我开始研究 2.8...
当然,可以通过一些导入来减少冗长的内容。首先,请注意
Map
在 2.8 上将是一个可变映射。好的一面是,如果您将地图转换回原图,您将获得原始对象。现在,我不知道为什么
Properties
实现Map
,因为 javadoc 明确指出键和值是String
,但就这样吧。必须对其进行类型转换使得隐式选项的吸引力大大降低。既然如此,替代方案是其中最简洁的。编辑
Scala 2.8 刚刚获得了从
Properties
到mutable.Map[String,String]
的隐式转换,这使得大部分代码毫无意义。Scala 2.7:
Though that needs some typecasting. Let me work on it a bit more.
Ok, that was easy. Let me work on 2.8 now...
The verbosity, of course, can be decreased with a couple of imports. First of all, note that
Map
will be a mutable map on 2.8. On the bright side, if you convert back the map, you'll get the original object.Now, I have no clue why
Properties
implementsMap<Object, Object>
, given that the javadocs clearly state that key and value areString
, but there you go. Having to typecast this makes the implicit option much less attractive. This being the case, the alternative is the most concise of them.EDIT
Scala 2.8 just acquired an implicit conversion from
Properties
tomutable.Map[String,String]
, which makes most of that code moot.在 Scala 2.9.1 中,这是通过 collection.JavaConversions._ 内的隐式转换解决的。其他答案使用已弃用的函数。详细信息记录在此处。这是该页面的相关片段:
从可变映射到不可变映射只需调用 toMap 即可。
In Scala 2.9.1 this is solved by implicit conversions inside collection.JavaConversions._ . The other answers use deprecated functions. The details are documented here. This is a relevant snippet out of that page:
Getting from a mutable map to an immutable map is a matter of calling toMap on it.
在 Scala 2.8.1 中,您可以使用
asScalaMap(m : java.util.Map[A, B])
以更简洁的方式完成此操作:In Scala 2.8.1 you can do it with
asScalaMap(m : java.util.Map[A, B])
in a more concise way:在 Scala 2.13.2 中:
In Scala 2.13.2:
看起来在最新版本的 Scala(截至本答案时为 2.10.2)中,首选方法是使用 scala.collection.JavaConverters 中的显式 .asScala :
Looks like in the most recent version of Scala (2.10.2 as of the time of this answer), the preferred way to do this is using the explicit
.asScala
fromscala.collection.JavaConverters
: