Removes all of the mappings from this map. The map will be empty after this call returns.
Program
package com.candidjava;
import java.util.Map;
import java.util.WeakHashMap;
/*
* @author : Mohan
* @description : Removes all of the mappings from this map using clear() method
*/
public class WeakHashMapClear {
public static void main(String[] args) {
Map<String, String> weakHashMap = new WeakHashMap<String, String>();
weakHashMap.put("0", "mohan");
weakHashMap.put("1", "karthi");
weakHashMap.put("2", "kamal");
weakHashMap.put("3", "hari");
System.out.println("Contents in the Map" + weakHashMap);
System.out.println("Clear the Map");
weakHashMap.clear();
System.out.println("Contents in the Map" + weakHashMap);
}
}
Output
Contents in the Map{0=mohan, 1=karthi, 2=kamal, 3=hari}
Clear the Map
Contents in the Map{}
Explanation
public void clear()
Removes all of the mappings from this map. The map will be empty after this call returns.