📜  带有示例的 Scala TreeSet toMap() 方法

📅  最后修改于: 2022-05-13 01:54:55.395000             🧑  作者: Mango

带有示例的 Scala TreeSet toMap() 方法

toMap()方法用于返回由 TreeSet 的所有元素组成的地图。

示例 #1:

// Scala program of toMap() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet((1, 2), (3, 4), (5, 6))  
            
        // Print the TreeSet 
        println(t1) 
          
        // Applying toMap method  
        val result = t1.toMap
            
        // Display output 
        print("Elements in the Map: " + result) 
            
    } 
} 
输出:
TreeSet((1, 2), (3, 4), (5, 6))
Elements in the Map: Map(1 -> 2, 3 -> 4, 5 -> 6)

示例 #2:

// Scala program of toMap() 
// method 
  
// Import TreeSet
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating TreeSet
        val t1 = TreeSet(("u", "a"), ("i", "o"), ("e", "f"))  
            
        // Print the TreeSet 
        println(t1) 
          
        // Applying toMap method  
        val result = t1.toMap
            
        // Display output 
        print("Elements in the Map: " + result) 
            
    } 
} 
输出:
TreeSet((e, f), (i, o), (u, a))
Elements in the Map: Map(e -> f, i -> o, u -> a)