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.
// 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)); } } |
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 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)); } } |
ConcurrentSkipListSet: [123, 200, 324, 400, 652] The returned elements are: [123, 200, 324, 400]
Recommended Posts:
- ConcurrentSkipListSet subSet() method in Java
- ConcurrentSkipListSet tailSet() method in Java with Examples
- ConcurrentSkipListSet lower() method in Java with Examples
- ConcurrentSkipListSet comparator() method in Java with Examples
- ConcurrentSkipListSet in Java with Examples
- ConcurrentSkipListSet last() method in Java
- ConcurrentSkipListSet first() method in Java
- ConcurrentSkipListSet add() method in Java
- ConcurrentSkipListSet contains() method in Java
- ConcurrentSkipListSet size() method in Java
- ConcurrentSkipListSet pollFirst() method in Java
- ConcurrentSkipListSet pollLast() method in Java
- ConcurrentSkipListSet removeAll() method in Java
- ConcurrentSkipListSet clone() method in Java
- ConcurrentSkipListSet isEmpty() method in Java
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.



