The clone() method is utilized to make a copy of the receivers object. value clone is a member of scala.collection.mutable.Map[String, Int].
Method Definition: def clone(): Map[A, B]
Return Type: It returns the copy of the map used.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = scala.collection.mutable.Map( "geeks" - > 5 , "for" - > 3 )
val result = m 1 .clone()
println(result)
}
}
|
Output:
Map(geeks -> 5, for -> 3)
Here, a mutable map is used as clone() is a member of scala.collection.mutable.Map[String, Int].
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = scala.collection.immutable.Map( "geeks" - > 5 , "for" - > 3 )
val result = m 1 .clone()
println(result)
}
}
|
Output:prog.scala:16: error: value clone is not a member of scala.collection.immutable.Map[String,Int]
val result = m1.clone()
^
one error found
Here, one error is found as immutable map is utilized but clone method is a member of scala.collection.mutable.Map[String, Int].