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:
我尝试使用java迭代器和/或枚举将环境变量引入到scala脚本中,并意识到Frankenstein博士可能会声称是parentage,所以我从这棵丑陋的树中删除了以下内容:
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 ;@)
请,请不要把这个td# d抛光,只要给我看看我确信存在的scala gem(我)。e java属性——> scala.Map),预先感谢;@)
5 个解决方案
#1
7
Scala 2.7:
Scala 2.7:
val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements
Though that needs some typecasting. Let me work on it a bit more.
尽管这需要一些类型的调整。让我再多做一点。
val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements.asInstanceOf[Iterator[(String, String)]]
Ok, that was easy. Let me work on 2.8 now...
好的,这很容易。让我现在做2.8。
import scala.collection.JavaConversions.asMap
val props = System.getProperties() : scala.collection.mutable.Map[AnyRef, AnyRef] // or
val props = System.getProperties().asInstanceOf[java.util.Map[String, String]] : scala.collection.mutable.Map[String, String] // way too many repetitions of types
val props = asMap(System.getProperties().asInstanceOf[java.util.Map[String, String]])
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.
当然,可以通过几个导入来减少冗余。首先,请注意,Map将是一个在2.8上的可变映射。在好的方面,如果你转换回地图,你会得到原来的对象。
Now, I have no clue why Properties
implements Map<Object, Object>
, given that the javadocs clearly state that key and value are String
, 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.
现在,我不知道为什么属性实现了Map ,>
EDIT
编辑
Scala 2.8 just acquired an implicit conversion from Properties
to mutable.Map[String,String]
, which makes most of that code moot.
Scala 2.8刚刚获得了从属性到可变的隐式转换。Map[String,String],这使得大部分代码都没有意义。
#2
7
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:
在Scala 2.9.1中,这是通过collection.javaconversion中的隐式转换来解决的。_。其他答案使用弃用函数。这里记录了详细信息。这是该页面的相关片段:
scala> import collection.JavaConversions._
import collection.JavaConversions._
scala> import collection.mutable._
import collection.mutable._
scala> val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3)
jul: java.util.List[Int] = [1, 2, 3]
scala> val buf: Seq[Int] = jul
buf: scala.collection.mutable.Seq[Int] = ArrayBuffer(1, 2, 3)
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2)
m: java.util.Map[String,Int] = {hello=2, abc=1}
Getting from a mutable map to an immutable map is a matter of calling toMap on it.
从可变映射到不可变映射是一个调用toMap的问题。
#3
4
In Scala 2.8.1 you can do it with asScalaMap(m : java.util.Map[A, B])
in a more concise way:
在Scala 2.8.1中,您可以使用asScalaMap(m: java.util)来完成它。用更简洁的方式映射[A, B]:
var props = asScalaMap(System.getProperties())
props.keySet.toList.sortWith(_ < _).foreach { k =>
println(k + (" " * (30 - k.length)) + " = " + props(k))
}
#4
4
Scala 2.10.3
Scala 2.10.3
import scala.collection.JavaConverters._
//Create a variable to store the properties in
val props = new Properties
//Open a file stream to read the file
val fileStream = new FileInputStream(new File(fileName))
props.load(fileStream)
fileStream.close()
//Print the contents of the properties file as a map
println(props.asScala.toMap)
#5
1
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
from scala.collection.JavaConverters
:
在最近的Scala版本中(这个答案的时间是2.10.2),最好的方法是使用Scala . collection.javaconverters的显式.asScala。
import scala.collection.JavaConverters._
val props = System.getProperties().asScala
assert(props.isInstanceOf[Map[String, String]])
#1
7
Scala 2.7:
Scala 2.7:
val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements
Though that needs some typecasting. Let me work on it a bit more.
尽管这需要一些类型的调整。让我再多做一点。
val props = Map() ++ scala.collection.jcl.Conversions.convertMap(System.getProperties).elements.asInstanceOf[Iterator[(String, String)]]
Ok, that was easy. Let me work on 2.8 now...
好的,这很容易。让我现在做2.8。
import scala.collection.JavaConversions.asMap
val props = System.getProperties() : scala.collection.mutable.Map[AnyRef, AnyRef] // or
val props = System.getProperties().asInstanceOf[java.util.Map[String, String]] : scala.collection.mutable.Map[String, String] // way too many repetitions of types
val props = asMap(System.getProperties().asInstanceOf[java.util.Map[String, String]])
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.
当然,可以通过几个导入来减少冗余。首先,请注意,Map将是一个在2.8上的可变映射。在好的方面,如果你转换回地图,你会得到原来的对象。
Now, I have no clue why Properties
implements Map<Object, Object>
, given that the javadocs clearly state that key and value are String
, 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.
现在,我不知道为什么属性实现了Map ,>
EDIT
编辑
Scala 2.8 just acquired an implicit conversion from Properties
to mutable.Map[String,String]
, which makes most of that code moot.
Scala 2.8刚刚获得了从属性到可变的隐式转换。Map[String,String],这使得大部分代码都没有意义。
#2
7
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:
在Scala 2.9.1中,这是通过collection.javaconversion中的隐式转换来解决的。_。其他答案使用弃用函数。这里记录了详细信息。这是该页面的相关片段:
scala> import collection.JavaConversions._
import collection.JavaConversions._
scala> import collection.mutable._
import collection.mutable._
scala> val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3)
jul: java.util.List[Int] = [1, 2, 3]
scala> val buf: Seq[Int] = jul
buf: scala.collection.mutable.Seq[Int] = ArrayBuffer(1, 2, 3)
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2)
m: java.util.Map[String,Int] = {hello=2, abc=1}
Getting from a mutable map to an immutable map is a matter of calling toMap on it.
从可变映射到不可变映射是一个调用toMap的问题。
#3
4
In Scala 2.8.1 you can do it with asScalaMap(m : java.util.Map[A, B])
in a more concise way:
在Scala 2.8.1中,您可以使用asScalaMap(m: java.util)来完成它。用更简洁的方式映射[A, B]:
var props = asScalaMap(System.getProperties())
props.keySet.toList.sortWith(_ < _).foreach { k =>
println(k + (" " * (30 - k.length)) + " = " + props(k))
}
#4
4
Scala 2.10.3
Scala 2.10.3
import scala.collection.JavaConverters._
//Create a variable to store the properties in
val props = new Properties
//Open a file stream to read the file
val fileStream = new FileInputStream(new File(fileName))
props.load(fileStream)
fileStream.close()
//Print the contents of the properties file as a map
println(props.asScala.toMap)
#5
1
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
from scala.collection.JavaConverters
:
在最近的Scala版本中(这个答案的时间是2.10.2),最好的方法是使用Scala . collection.javaconverters的显式.asScala。
import scala.collection.JavaConverters._
val props = System.getProperties().asScala
assert(props.isInstanceOf[Map[String, String]])