Scala Mutable SortedSet toMap() 方法

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 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程