Skip to content
Related Articles

Related Articles

Scala Map copyToArray() method with example
  • Last Updated : 13 Aug, 2019
GeeksforGeeks - Summer Carnival Banner

The copyToArray() method is utilized in copying pair of keys of the Map to an Array.

Method Definition: def copyToArray(xs: Array[(A, B)]): Unit

Return Type: It returns the keys of the map to an array.

Example #1:




// Scala program of copyToArray()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val m1 = Map("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:



(geeks, 5)
(for, 3)
(cs, 2)
0
0

So, here the keys are copied to the array.
Example #2:




// Scala program of copyToArray()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a map
        val m1 = Map("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:
(geeks, 5)
(for, 3)
0
0
0

Here, the identical keys are removed.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :