What is the map.clear() method in Scala?
The map.clear() function in Scala is used to remove all elements (key-value pairs) in the map.
The illustration below shows the visual representation of the map.clear() function.
The following module is required in order to use the function
map.clear().
scala.collection.mutable.Map
Syntax
map_name.clear()
map_nameis the name of the map.
Parameters
The map.clear() function does not require any parameters.
Return value
Scala’s map.clear() function is used to erase all of the map’s elements (key-value pairs).
Code
The code below shows how to use the map.clear() function in Scala.
import scala.collection.mutable.Mapobject Main extends App {//creating mapval map = scala.collection.mutable.Map(1 -> "Tom",2 -> "Alsvin",3 -> "Eddie")//map containg key value pairs before clearprintln("The map elements before clear: " + map);//map after clearmap.clear();println("The map elements after clear: " + map);}