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.

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_name is 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.Map
object Main extends App {
//creating map
val map = scala.collection.mutable.Map(
1 -> "Tom",
2 -> "Alsvin",
3 -> "Eddie"
)
//map containg key value pairs before clear
println("The map elements before clear: " + map);
//map after clear
map.clear();
println("The map elements after clear: " + map);
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources