Related Articles

Related Articles

Scala Set toMap() method with example
  • Last Updated : 18 Oct, 2019

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

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

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

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of toMap() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set((1, 2), (3, 4), (5, 6)) 
          
        // Applying toMap method 
        val result = s1.toMap
          
        // Display output
        println(result)
              
    

chevron_right


Output:

Map(1 -> 2, 3 -> 4, 5 -> 6)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of toMap() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set((12, 2), (13, 4), (15, 6)) 
          
        // Applying toMap method 
        val result = s1.toMap
          
        // Display output
        println(result)
              
    

chevron_right


Output:

Map(12 -> 2, 13 -> 4, 15 -> 6)



My Personal Notes arrow_drop_up
Recommended Articles
Page :