Scala SortedSet toMap()函数及其示例
toMap() 函数用于返回由SortedSet中所有元素组成的映射。
函数定义:def toMap[T, U]: Map[T, U]
返回类型:返回由SortedSet中所有元素组成的映射。
示例1:
// Scala程序 toMap()
// 函数
import scala.collection.immutable.SortedSet
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建SortedSet
val s1 = SortedSet((1, 2), (3, 4), (5, 6))
// 应用toMap函数
val result = s1.toMap
// 显示输出
println(result)
}
}
Map(1 -> 2, 3 -> 4, 5 -> 6)
示例2:
// Scala程序 toMap()
// 函数
import scala.collection.immutable.SortedSet
// 创建对象
object GfG
{
// 主方法
def main(args:Array[String])
{
// 创建SortedSet
val s1 = SortedSet((12, 2), (13, 4), (15, 6))
// 应用toMap函数
val result = s1.toMap
// 显示输出
println(result)
}
}
Map(12 -> 2, 13 -> 4, 15 -> 6)
阅读更多:Scala 教程