java.util.HashMap.clear() 方法
描述
clear() 方法用于从该映射中删除所有映射。
声明
以下是 java.util.HashMap.clear() 方法的声明。
public void clear()
参数
NA
返回值
NA
异常
NA
示例
下面的例子展示了 java.util.HashMap.clear() 的用法。
package com.tutorialspoint;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
System.out.println("Initial map elements: " + newmap);
// clear hash map
newmap.clear();
System.out.println("Map elements after clear: " + newmap);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Initial map elements: {1=tutorials, 2=point, 3=is best}
Map elements after clear: {}

