ConcurrentSkipListSet descendingSet() method in Java
Last Updated :
17 Sep, 2018
The descendingSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa.
Syntax:
ConcurrentSkipListSet.descendingSet()
Return Value: The function returns a NavigableSet which is a reverse order view of this set.
Below programs illustrate the ConcurrentSkipListSet.Set() method:
Program 1:
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetIteratorExample1 {
public static void main(String[] args)
{
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);
NavigableSet<Integer> des_set = set.descendingSet();
System.out.println( "Contents of the descending set: "
+ des_set);
}
}
|
Output:
Contents of the set: [10, 20, 25, 35]
Contents of the descending set: [35, 25, 20, 10]
Program 2: Program demonstrating change in the original set while insertion of elements in the descending set.
import java.util.NavigableSet;
import java.util.Iterator;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetIteratorExample2 {
public static void main(String[] args)
{
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);
}
}
|
Output:
Contents of the set: [alex, bob, chuck, drake, eric, fred]
Contents of the descending set: [fred, eric, drake, chuck, bob, alex]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#descendingSet–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...