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) } } |
chevron_right
filter_none
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) } } |
chevron_right
filter_none
Output:
(geeks, 5) (for, 3) 0 0 0
Here, the identical keys are removed.
Recommended Posts:
- Scala Set copyToArray() method with example
- Scala Queue copyToArray() method with example
- Scala SortedSet copyToArray() method with example
- Scala SortedMap copyToArray() method with example
- Scala Stack copyToArray() method with example
- Scala TreeSet copyToArray() method with example
- Scala Mutable SortedMap copyToArray() method with example
- Scala BitSet copyToArray() method with example
- Scala Mutable SortedSet copyToArray() method
- Scala immutable TreeSet copyToArray() method
- Scala BitSet copyToArray(xs, start, len) method
- Scala Map size() method with example
- Scala Map toSet() method with example
- Scala map contains() method with example
- Scala map isDefinedAt() method with example
- Scala Iterator map() method with example
- Scala Map clone() method with example
- Scala Map clear() method with example
- Scala Map apply() method with example
- Scala Map addString() method with a separator with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

