Scala Mutable SortedSet toMap() 方法
在 Scala 的可变集合中, toMap() 方法用于返回一个由 SortedSet 的所有元素组成的 Map。
方法定义:def toMap[T, U]: Map[T, U]
返回类型:返回一个由 SortedSet 的所有元素组成的 Map。
例子 #1:
// Scala toMap() 方法的程序
// 导入可变的 SortedSet 集合包
import scala.collection.mutable.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() 方法的程序
// 导入可变的 SortedSet 集合包
import scala.collection.mutable.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 教程
极客教程