Scala Java字符集转换为Scala序列的程序
通过在Scala中使用Java的toSeq方法,可以将Java字符集转换为Scala序列。在这里,我们需要导入Scala的JavaConversions对象以使此转换工作。
现在,让我们看一些示例,然后再讨论它的详细工作原理。
示例1:
// Scala program to convert Java set
// 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 set of character in Java
val set = new java.util.HashSet[Char]()
// Adding characters to the set
set.add('a')
set.add('b')
set.add('c')
// Convert set to Sequence
val seq = set.toSeq
// Displays Sequence
println(seq)
}
}
ArrayBuffer(a, b, c)
示例2:
// Scala program to convert Java set
// 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 set of character in Java
val set = new java.util.HashSet[Char]()
// Adding characters to the set
set.add('d')
set.add('b')
set.add('c')
// Convert set to Sequence
val seq = set.toSeq
// Displays Sequence
println(seq)
}
}
ArrayBuffer(b, c, d)