Java TreeMap clear()方法

java.util.TreeMap.clear() 删除Map的所有键值对。

1 语法

public void clear()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.TreeMap.clear()方法的例子
 */
import java.util.*;

public class Demo {
    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: {}

热门文章

优秀文章