Skip to content
Related Articles

Related Articles

Scala Map clone() method with example
  • Last Updated : 13 Aug, 2019

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 m1 = scala.collection.mutable.Map("geeks" -> 5, "for" -> 3)
          
        // Applying clone method
        val result = m1.clone()
          
        // Displays output
        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:




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

My Personal Notes arrow_drop_up
Recommended Articles
Page :