ConcurrentSkipListSet subSet() method in Java

subSet(E fromElement, E toElement)

The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned set is empty.) The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.

Syntax:

public NavigableSet subSet(E fromElement,
                     E toElement)

Parameter: The function accepts the following parameters:

  • fromElement – low endpoint (inclusive) of the returned set.
  • toElement – high endpoint (exclusive) of the returned set
  • Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

    Exception: The function throws the following exceptions:

  • ClassCastException – if fromElement and toElement cannot be compared to one another using this set’s comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
  • NullPointerException – if fromElement or toElement is null
  • IllegalArgumentException – if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range.
  • Below programs illustrate the ConcurrentSkipListSet.subSet() method:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate subSet()
    // method of ConcurrentSkipListSet
      
    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetSubSetExample1 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet<Integer>
                set = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 0; i <= 10; i++)
                set.add(i);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            // Creating a subsetset object
            NavigableSet<Integer> sub_set = set.subSet(2, 8);
      
            // Printing the elements of the descending set
            System.out.println("Contents of the subset: " + sub_set);
        }
    }

    chevron_right

    
    

    Output:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Contents of the subset: [2, 3, 4, 5, 6, 7]
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate subSet()
    // method of ConcurrentSkipListSet
      
    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetSubSetExample2 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet<Integer>
                set = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 0; i <= 10; i++)
                set.add(i);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            try {
                // Creating a subsetset object
                NavigableSet<Integer> sub_set = set.subSet(2, null);
            }
            catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }
    }

    chevron_right

    
    

    Output:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Exception: java.lang.NullPointerException
    

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

    The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns a view of the portion of this set whose elements range from fromElement to toElement. If fromElement and toElement are equal, the returned set is empty unless fromInclusive and toInclusive are both true. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports. The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

    Syntax:

    public NavigableSet subSet(E fromElement,
                         boolean fromInclusive,
                         E toElement,
                         boolean toInclusive)
    

    Parameter: The function accepts the following parameters:

  • fromElement – low endpoint of the returned set
  • fromInclusive – true if the low endpoint is to be included in the returned view
  • toElement – high endpoint of the returned set
  • toInclusive – true if the high endpoint is to be included in the returned view
  • Return Value: The function returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

    Exception: The function throws the following exceptions:

  • ClassCastException – if fromElement and toElement cannot be compared to one another using this set’s comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception if fromElement or toElement cannot be compared to elements currently in the set.
  • NullPointerException – if fromElement or toElement is null
  • IllegalArgumentException – if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement or toElement lies outside the bounds of the range.
  • Below programs illustrate the ConcurrentSkipListSet.subSet() method:
    Program 3:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate subSet()
    // method of ConcurrentSkipListSet */
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetSubSetExample3 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet<Integer>
                set = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 0; i <= 10; i++)
                set.add(i);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            // Creating a subsetset object
            NavigableSet<Integer> sub_set = set.subSet(2, true, 8, true);
      
            // Printing the elements of the descending set
            System.out.println("Contents of the subset: " + sub_set);
        }
    }

    chevron_right

    
    

    Output:

    Contents of the set: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Contents of the subset: [2, 3, 4, 5, 6, 7, 8]
    

    Reference:
    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#subSet-E-E-

    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#subSet-E-boolean-E-boolean-



    My Personal Notes arrow_drop_up

    Coder Machine Learner Social Activist Vocalist

    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.