Open In App

Scala SortedMap copyToArray() method with example

Last Updated : 03 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The copyToArray() method is utilized in copying pair of keys of the SortedMap to an Array.
Method Definition: def copyToArray(xs: Array[(A, B)]): Unit Return Type: It returns the keys of the SortedMap to an array.
Example #1: Scala
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedMap

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a SortedMap
        val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2)
        
        // Creating Array 
        val x: Array[Any] = Array(0, 0, 0, 0, 0) 
        
        // using 'copyToArray' method 
        m1.copyToArray(x) 
        
        // Displays keys copied in 
        // the Array 
        for(m2 <-x) 
        println(m2) 
    }
}
Output:
(cs, 2)
(for, 3)
(geeks, 5)
0
0
So, here the keys are copied to the array. Example #2: Scala
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedMap

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a SortedMap
        val m1 = SortedMap("geeks" -> 5, "for" -> 3, "geeks" -> 5)
        
        // Creating Array 
        val x: Array[Any] = Array(0, 0, 0, 0, 0) 
        
        // using 'copyToArray' method 
        m1.copyToArray(x) 
        
        // Displays keys copied in 
        // the Array 
        for(m2 <-x) 
        println(m2) 
    }
}
Output:
(for, 3)
(geeks, 5)
0
0
0
Here, the identical keys are removed.

Next Article
Article Tags :

Similar Reads