Java TreeSet clear()方法

java.util.TreeSet.clear() 方法用于删除集合中的所有元素。

1 语法

public void clear()

2 参数

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) {

        // creating a TreeSet
        TreeSet <Integer>treeadd = new TreeSet<Integer>();

        // adding in the tree set
        treeadd.add(12);
        treeadd.add(13);
        treeadd.add(14);
        treeadd.add(15);

        // create an iterator
        Iterator iterator;
        iterator = treeadd.iterator();

        // displaying the Tree set data
        System.out.println("Tree set data: ");

        while (iterator.hasNext()) {
            System.out.println(iterator.next() + " ");
        }

        // clearing the set
        System.out.println("Clearing tree");
        treeadd.clear();
        System.out.println("Size of the tree: "+treeadd.size());
    }
}

输出结果为:

Tree set data: 
12 
13 
14 
15 
Clearing tree
Size of the tree: 0

热门文章

优秀文章