Skip to content
Related Articles

Related Articles

ConcurrentSkipListSet subSet() method in Java with Examples

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 17 Nov, 2021

The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java where the elements are returned in a range defined by this method. 
The syntax of the function gives a clear understanding of the specified element followed by the examples.
Syntax: 
 

subSet(E fromElement, E toElement)
           or 
subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)

Parameters: 
The first variation of this method takes two parameters, which defines the range of elements that are going to be returned. The toElement is not included in the returned set.
The second variation is similar to first one but here the boolean parameters specifically include or exclude the toElement and fromElement. 
Returns: 
The first method variation returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. 
The second method variation returns a view of the portion of this set whose elements range from fromElement to toElements as defined by the boolean inclusive parameter.
Exception: 
Null Pointer Exception: if the specified elements are NULL.
Below are the sample programs to illustrate ConcurrentSkipListSet subSet() in Java:
Example: 1 
The elements from 30000 to 90000 are returned, but 90000 is excluded.
 

Java




// Java program to demonstrate ConcurrentSkipListSet subSet() method
 
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetExample {
    public static void main(String[] args)
    {
 
        // Initializing the set using ConcurrentSkipListSet()
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
 
        // Adding elements to this set
        set.add(65552);
        set.add(34324);
        set.add(93423);
        set.add(41523);
        set.add(90000);
 
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
 
        // Printing the elements of ConcurrentSkipListSet that
        // are returned by subSet() method
        System.out.println("The returned elements are: "
                           + set.subSet(30000, 90000));
    }
}

Output: 

ConcurrentSkipListSet: [34324, 41523, 65552, 90000, 93423]
The returned elements are: [34324, 41523, 65552]

 

Example: 2 
In this example, element 400 is also returned because boolean inclusive is true. 
 

Java




// Java program to demonstrate ConcurrentSkipListSet subSet() method
 
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetExample {
    public static void main(String[] args)
    {
 
        // Initializing the set using ConcurrentSkipListSet()
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
 
        // Adding elements to this set
        set.add(652);
        set.add(324);
        set.add(400);
        set.add(123);
        set.add(200);
 
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
 
        // Printing the elements of ConcurrentSkipListSet that
        // are returned by subSet() method
        System.out.println("The returned elements are: "
                           + set.subSet(100, true, 400, true));
    }
}

Output: 

ConcurrentSkipListSet: [123, 200, 324, 400, 652]
The returned elements are: [123, 200, 324, 400]

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!