Scala Map toSet()方法的示例

Scala Map toSet()方法的示例

toSet()方法被用来展示Scala中映射的集合。

方法定义:def toSet: Set[A]

返回类型:它从指定的映射中返回一个集合。

示例1:

// Scala program of toSet()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val m1 = Map(3 -> "geeks", 4 -> "for", 4 -> "for")
          
        // Applying toSet method
        val result = m1.toSet
          
        // Displays output
        println(result)
          
    }
}
Set((3, geeks), (4, for))

示例2:

// Scala program of toSet()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val m1 = Map(3 -> "geeks", 4 -> "for", 2 -> "cs")
          
        // Applying toSet method
        val result = m1.toSet
          
        // Displays output
        println(result)
          
    }
}
Set((3, geeks), (4, for), (2, cs))

阅读更多:Scala 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程