📜  Java中的 TreeSet descendingSet() 方法及示例

📅  最后修改于: 2022-05-13 01:55:34.755000             🧑  作者: Mango

Java中的 TreeSet descendingSet() 方法及示例

Java.util.TreeSet类的descendingSet()方法用于返回此集合中包含的元素的逆序视图。降序集由该集支持,因此对集的更改会反映在降序集中,反之亦然。如果在对任一集进行迭代时修改了任一集(通过迭代器自己的删除操作除外),则迭代的结果是未定义的。
返回的集合具有等价于 Collections.reverseOrder(comparator()) 的排序。表达式 s.descendingSet().descendingSet() 返回一个与 s 基本等价的 s 视图。
句法:

public NavigableSet descendingSet()

返回值:此方法返回此集合的逆序视图
下面是说明descendingSet()方法的例子
示例 1:

Java
// Java program to demonstrate
// descendingSet() method
// for String value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
 
            // create tree set object
            TreeSet treeadd = new TreeSet();
 
            // populate the TreeSet using add() method
            treeadd.add("A");
            treeadd.add("B");
            treeadd.add("C");
            treeadd.add("D");
 
            // Print the TreeSet
            System.out.println("TreeSet: " + treeadd);
 
            // getting the reverse order view of element
            // using descendingSet() method
            NavigableSet
                treereverse = treeadd.descendingSet();
 
            // getting iterated view of NavigableSet
            Iterator iterator = treereverse.iterator();
 
            System.out.println("\nValues using DescendingSet:");
 
            // printing the integrated value
            while (iterator.hasNext()) {
                System.out.println("Value : "
                                   + iterator.next());
            }
        }
 
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// descendingSet() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // create tree set object
            TreeSet treeadd = new TreeSet();
 
            // populate the TreeSet using add() method
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
 
            // getting the reverse order view of element
            // using descendingSet() method
            NavigableSet treereverse = treeadd.descendingSet();
 
            // getting iterated view of NavigableSet
            Iterator iterator = treereverse.iterator();
 
            System.out.println("\nValues using DescendingSet:");
 
            // printing the integrated value
            while (iterator.hasNext()) {
                System.out.println("Value : " + iterator.next());
            }
        }
 
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
TreeSet: [A, B, C, D]

Values using DescendingSet:
Value : D
Value : C
Value : B
Value : A

示例 2:

Java

// Java program to demonstrate
// descendingSet() method
// for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
 
            // create tree set object
            TreeSet treeadd = new TreeSet();
 
            // populate the TreeSet using add() method
            treeadd.add(10);
            treeadd.add(20);
            treeadd.add(30);
            treeadd.add(40);
 
            // getting the reverse order view of element
            // using descendingSet() method
            NavigableSet treereverse = treeadd.descendingSet();
 
            // getting iterated view of NavigableSet
            Iterator iterator = treereverse.iterator();
 
            System.out.println("\nValues using DescendingSet:");
 
            // printing the integrated value
            while (iterator.hasNext()) {
                System.out.println("Value : " + iterator.next());
            }
        }
 
        catch (NullPointerException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Values using DescendingSet:
Value : 40
Value : 30
Value : 20
Value : 10