Scala SortedSet toMap()函数及其示例

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)
              
    } 
} 
Scala
Map(1 -> 2, 3 -> 4, 5 -> 6)
Scala

示例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)
              
    } 
} 
Scala
Map(12 -> 2, 13 -> 4, 15 -> 6)
Scala

阅读更多:Scala 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册