Open In App

Scala Map clone() method with example

Last Updated : 13 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
// 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
// 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].

Similar Reads