java.util.WeakHashMap.clear() 方法
描述
clear() 方法用于从该映射中删除所有映射。
声明
以下是 java.util.WeakHashMap.clear() 方法的声明。
public void clear()
参数
NA
返回值
NA
异常
NA
示例
下面的例子展示了 java.util.WeakHashMap.clear() 方法的使用。
package com.tutorialspoint;
import java.util.Map;
import java.util.WeakHashMap;
public class WeakHashMapDemo {
public static void main(String[] args) {
Map<String, String> weakHashMap = new WeakHashMap<String, String>();
// put keys and values in the Map
weakHashMap.put("1", "first");
weakHashMap.put("2", "two");
weakHashMap.put("3", "three");
// print the contents
System.out.println("Contents in the Map");
System.out.println(weakHashMap);
// call clear() method
System.out.println("Clear the Map");
weakHashMap.clear();
// again print the contents
System.out.println("Contents in the Map");
System.out.println(weakHashMap);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Contents in the Map {1=first, 2=two, 3=three} Clear the Map Contents in the Map {}