Scala Map clone() method with example
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:
// Scala program of clone() // method // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating a map val m 1 = scala.collection.mutable.Map( "Beginner" - > 5 , "for" - > 3 ) // Applying clone method val result = m 1 .clone() // Displays output println(result) } } |
Output:
Map(Beginner -> 5, for -> 3)
Here, a mutable map is used as clone() is a member of scala.collection.mutable.Map[String, Int].
Example #2:
// Scala program of clone() // method // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating a map val m 1 = scala.collection.immutable.Map( "Beginner" - > 5 , "for" - > 3 ) // Applying clone method val result = m 1 .clone() // Displays output 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].
Contact Us