ConcurrentSkipListSet subSet() method in Java with Examples

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 retured by subSet() method
        System.out.println("The returned elements are: "
                           + set.subSet(30000, 90000));
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 retured by subSet() method
        System.out.println("The returned elements are: "
                           + set.subSet(100, true, 400, true));
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.