The clear
method removes all entries of HashMap
.
fun clear()
This method doesn’t take any argument.
This method doesn’t return any value.
The code below demonstrates how to remove all entries of HashMap
:
fun main() {//create a new LinkedHashMap which can have String type as key, and int type as valuevar map : HashMap<String, Int> = HashMap<String, Int> ()// add two entriesmap.put("one", 1)map.put("two", 2)map.put("three", 3)println("The map is : $map")map.clear();println("The map is : $map")}
HashMap
object named map
.map
, {one=1,two=2,three=3}
, using the put
method.map
object’s clear
method. This removes all the entries of the map
. Now, the map becomes empty {}
.