Skip to content
Related Articles

Related Articles

Improve Article
ConcurrentSkipListSet subSet() method in Java with Examples
  • Last Updated : 27 Dec, 2018

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));
    }
}
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 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));
    }
}
Output:
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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :