Scala 将Java的double列表转换为Scala中的Seq的程序
利用Scala中Java的toSeq方法,可以将Java的double列表转换为Scala中的序列。在此,我们需要导入Scala的JavaConversions对象,以使该转换工作,否则将会出现错误。
现在,让我们看一些示例,然后讨论它如何详细工作。
Example:1#
// Scala program to convert Java list
// to Sequence in Scala
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating list of doubles in Java
val list = new java.util.ArrayList[Double]()
// Adding doubles to the list
list.add(4.34)
list.add(51.34)
list.add(2.34)
// Converting list to Sequence
val seq= list.toSeq
// Displays seq
println(seq)
}
}
Buffer(4.34, 51.34, 2.34)
Example:2#
// Scala program to convert Java list
// to Sequence in Scala
// Importing Scala's JavaConversions object
import scala.collection.JavaConversions._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating list of doubles in Java
val list = new java.util.ArrayList[Double]()
// Adding doubles to the list
list.add(4.15)
list.add(4.45)
list.add(4.11)
// Converting list to Sequence
val seq= list.toSeq
// Displays seq
println(seq)
}
}
Buffer(4.15, 4.45, 4.11)
Reference:
https://alvinalexander.com/scala/how-to-go-from-java-collections-convert-in-scala-interact .