Java中的ConcurrentSkipListSet descendingSet()方法

Java中的ConcurrentSkipListSet descendingSet()方法

java.util.concurrent.ConcurrentSkipListSet 类的 descendingSet() 方法是Java中的一种内置函数,它返回这个集合中元素的反向顺序视图。降序集合由这个集合支持,因此对集合的更改会反映在降序集合中,反之亦然。

语法:

ConcurrentSkipListSet.descendingSet()

返回值: 该函数返回一个导航集,它是这个集合的反向顺序视图。

下面的程序演示了ConcurrentSkipListSet.Set()方法:

程序1:

//Java程序演示ConcurrentSkipListSet的descendingSet()方法
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetIteratorExample1 {
    public static void main(String[] args)
    {
 
        // 初始化set
        ConcurrentSkipListSet<;Integer>;
            set = new ConcurrentSkipListSet<;Integer>;
 
        // 将元素添加到这个集合中
        set.add(10);
        set.add(35);
        set.add(20);
        set.add(25);
 
        // 打印集合的元素
        System.out.println("Contents of the set: " + set);
 
        // 创建一个降序的set对象
        NavigableSet<;Integer>; des_set = set.descendingSet();
 
        // 打印降序集合的元素
        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程序演示ConcurrentSkipListSet的descendingSet()方法
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetIteratorExample2 {
    public static void main(String[] args)
    {
 
        // 初始化set
        ConcurrentSkipListSet<;String>;
            set = new ConcurrentSkipListSet<;String>;
 
        // 将元素添加到这个集合中
        set.add("bob");
        set.add("alex");
        set.add("eric");
        set.add("chuck");
 
        // 创建一个降序对象
        NavigableSet<;String>; des_set = set.descendingSet();
 
        // 将元素添加到降序集合中,同时也添加到集合中
        des_set.add("drake");
        des_set.add("fred");
 
        // 打印集合的元素
        System.out.println("Contents of the set: " + 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingSet–

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程