subSet(E fromElement, E toElement)
The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.
Syntax:
public NavigableSet subSet(E fromElement, E toElement)
Parameter: The function accepts the following parameters:
Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
Exception: The function throws the following exceptions:
Below programs illustrate the ConcurrentSkipListSet.subSet() method:
Program 1:
// Java program to demonstrate subSet() // method of ConcurrentSkipListSet // Java Program Demonstrate subSet() // method of ConcurrentSkipListSet */ import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetSubSetExample1 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for ( int i = 0 ; i <= 10 ; i++)
set.add(i);
// Printing the elements of the set
System.out.println( "Contents of the set: " + set);
// Creating a subsetset object
NavigableSet<Integer> sub_set = set.subSet( 2 , 8 );
// Printing the elements of the descending set
System.out.println( "Contents of the subset: " + sub_set);
}
} |
Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Contents of the subset: [2, 3, 4, 5, 6, 7]
Program 2:
// Java program to demonstrate subSet() // method of ConcurrentSkipListSet // Java Program Demonstrate subSet() // method of ConcurrentSkipListSet */ import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetSubSetExample2 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for ( int i = 0 ; i <= 10 ; i++)
set.add(i);
// Printing the elements of the set
System.out.println( "Contents of the set: " + set);
try {
// Creating a subsetset object
NavigableSet<Integer> sub_set = set.subSet( 2 , null );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
} |
Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Exception: java.lang.NullPointerException
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns a view of the portion of this set whose elements range from fromElement to toElement. If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports. The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
Syntax:
public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
Parameter: The function accepts the following parameters:
Return Value: The function returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
Exception: The function throws the following exceptions:
Below programs illustrate the ConcurrentSkipListSet.subSet() method:
Program 3:
// Java Program Demonstrate subSet() // method of ConcurrentSkipListSet */ import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
class ConcurrentSkipListSetSubSetExample3 {
public static void main(String[] args)
{
// Initializing the set
ConcurrentSkipListSet<Integer>
set = new ConcurrentSkipListSet<Integer>();
// Adding elements to this set
for ( int i = 0 ; i <= 10 ; i++)
set.add(i);
// Printing the elements of the set
System.out.println( "Contents of the set: " + set);
// Creating a subsetset object
NavigableSet<Integer> sub_set = set.subSet( 2 , true , 8 , true );
// Printing the elements of the descending set
System.out.println( "Contents of the subset: " + sub_set);
}
} |
Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Contents of the subset: [2, 3, 4, 5, 6, 7, 8]