Related Articles

Related Articles

Scala TreeSet toMap() method with example
  • Last Updated : 11 Nov, 2019

The toMap() method is utilized to return a map consisting of all the elements of the TreeSet.

Method Definition: def toMap[T, U]: Map[T, U]

Return Type: It returns a map consisting of all the elements of the TreeSet.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 
            
    

chevron_right


Output:

TreeSet((1, 2), (3, 4), (5, 6))
Elements in the Map: Map(1 -> 2, 3 -> 4, 5 -> 6)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 
            
    

chevron_right


Output:

TreeSet((e, f), (i, o), (u, a))
Elements in the Map: Map(e -> f, i -> o, u -> a)



My Personal Notes arrow_drop_up
Recommended Articles
Page :