Java ConcurrentSkipListSet descendingSet()方法
java.util.concurrent.ConcurrentSkipListSet 的 descendingSet() 方法是Java中的一个内置函数,它返回这个集合中包含的元素的反向顺序视图。降序集合是由这个集合支持的,所以这个集合的变化会反映在降序集合中,反之亦然。
语法
ConcurrentSkipListSet.descendingSet()
返回值: 该函数返回一个NavigableSet,它是这个集合的反向顺序视图。
下面的程序说明了ConcurrentSkipListSet.Set()方法。
程序1 :
// 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<Integer>
set = new ConcurrentSkipListSet<Integer>();
// 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<Integer> 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<String>
set = new ConcurrentSkipListSet<String>();
// Adding elements to this set
set.add("bob");
set.add("alex");
set.add("eric");
set.add("chuck");
// Creating a descending object
NavigableSet<String> 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingSet-