Scala Map copyToArray() method with example
Last Updated :
13 Aug, 2019
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:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( "geeks" - > 5 , "for" - > 3 , "cs" - > 2 )
val x : Array[Any] = Array( 0 , 0 , 0 , 0 , 0 )
m 1 .copyToArray(x)
for (m 2 < -x)
println(m 2 )
}
}
|
Output:
(geeks, 5)
(for, 3)
(cs, 2)
0
0
So, here the keys are copied to the array.
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( "geeks" - > 5 , "for" - > 3 , "geeks" - > 5 )
val x : Array[Any] = Array( 0 , 0 , 0 , 0 , 0 )
m 1 .copyToArray(x)
for (m 2 < -x)
println(m 2 )
}
}
|
Output:
(geeks, 5)
(for, 3)
0
0
0
Here, the identical keys are removed.
Share your thoughts in the comments
Please Login to comment...