java.util.TreeMap.clear() 方法
描述
clear() 方法用于从该映射中删除所有映射。 此调用返回后,映射将为空。
声明
以下是 java.util.TreeMap.clear() 方法的声明。
public void clear()
参数
NA
返回值
NA
异常
NA
示例
下面的例子展示了 java.util.TreeMap.clear() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree map
NavigableMap<Integer, String> treemap = new TreeMap<Integer, String>();
// populating tree map
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
System.out.println("Entries in the Map: "+ treemap);
// clearing the map
System.out.println("Clearing the Map");
treemap.clear();
System.out.println("Entries in the Map: "+ treemap);
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Entries in the Map: {1=one, 2=two, 3=three, 5=five, 6=six}
Clearing the Map
Entries in the Map: {}

