📌  相关文章
📜  Java中的 ConcurrentSkipListSet descendingSet() 方法

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

Java中的 ConcurrentSkipListSet descendingSet() 方法

Java .util.concurrent.ConcurrentSkipListSetdescendingSet()方法是Java中的一个内置函数,它返回此集合中包含的元素的逆序视图。降序集由该集支持,因此对集的更改会反映在降序集中,反之亦然。

句法:

ConcurrentSkipListSet.descendingSet()

返回值:该函数返回一个 NavigableSet,它是该集合的逆序视图。

下面的程序说明了 ConcurrentSkipListSet.Set() 方法:

方案一:

// Java Program Demonstrate descendingSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetIteratorExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add(10);
        set.add(35);
        set.add(20);
        set.add(25);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Creating a descending set object
        NavigableSet des_set = set.descendingSet();
  
        // Printing the elements of the descending set
        System.out.println("Contents of the descending set: " 
                                                  + des_set);
    }
}
输出:
Contents of the set: [10, 20, 25, 35]
Contents of the descending set: [35, 25, 20, 10]

程序 2:演示在下降集合中插入元素时原始集合发生变化的程序。

// Java Program Demonstrate descendingSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetIteratorExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet
            set = new ConcurrentSkipListSet();
  
        // Adding elements to this set
        set.add("bob");
        set.add("alex");
        set.add("eric");
        set.add("chuck");
  
        // Creating a descending object
        NavigableSet des_set = set.descendingSet();
  
        // Adding elements to the descending set and also to the set
        des_set.add("drake");
        des_set.add("fred");
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Printing the elements of the descending set
        System.out.println("Contents of the descending set: " 
                                                  + des_set);
    }
}
输出:
Contents of the set: [alex, bob, chuck, drake, eric, fred]
Contents of the descending set: [fred, eric, drake, chuck, bob, alex]

参考: https: Java/util/concurrent/ConcurrentSkipListSet.html#descendingSet–