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.
subSet(E fromElement, E toElement) or subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
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.
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.
Null Pointer Exception: if the specified elements are NULL.
Below are the sample programs to illustrate ConcurrentSkipListSet subSet() in Java:
The elements from 30000 to 90000 are returned, but 90000 is excluded.
ConcurrentSkipListSet: [34324, 41523, 65552, 90000, 93423] The returned elements are: [34324, 41523, 65552]
In this example, element 400 is also returned because boolean inclusive is true.
ConcurrentSkipListSet: [123, 200, 324, 400, 652] The returned elements are: [123, 200, 324, 400]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.