Java TreeSet descendingSet()方法

java.util.TreeSet.descendingSet() 方法用于以相反的顺序返回元素。

1 语法

public NavigableSet<E> descendingSet()

2 参数

3 返回值

返回此集合的逆序视图。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.TreeSet.descendingSet()方法的例子
 */
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>();
        TreeSet <Integer>treereverse = new TreeSet<Integer>();

        // adding in the tree set
        treeadd.add(1);
        treeadd.add(13);
        treeadd.add(17);
        treeadd.add(2);

        // creating reverse set
        treereverse = (TreeSet)treeadd.descendingSet();

        // create descending set
        Iterator iterator;
        iterator = treereverse.iterator();

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

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

输出结果为:

Tree set data in reverse order: 
17 
13 
2 
1 

热门文章

优秀文章