Scala 将Java数组转换为Scala流的程序
通过利用Scala中Java的toStream方法,可以将Java的双精度Set转换为Scala中的流。在这里,我们需要导入Scala的JavaConversions对象以使此转换工作正常。
现在,让我们看一些示例,然后讨论它是如何工作的。
例子:1#
// Scala program to convert Java set
// to Stream in Scala
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating set of doubles in Java
val set = new java.util.HashSet[Double]()
// Adding doubles to the set
set.add(4.53)
set.add(122.53)
set.add(90.53)
// Converting set to Stream
val stream= set.toStream
// Displays Stream
println(stream)
}
}
Stream(90.53, ?)
例子:2#
// Scala program to convert Java set
// to Stream in Scala
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating set of doubles in Java
val set = new java.util.HashSet[Double]()
// Adding doubles to the set
set.add(12.08)
set.add(12.456)
set.add(12.11)
// Converting set to Stream
val stream= set.toStream
// Displays Stream
println(stream)
}
}
Stream(12.11, ?)