Open In App

ConcurrentSkipListSet subSet() method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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:




    // 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);
        }
    }

    
    

    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:




    // 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);
            }
        }
    }

    
    

    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:




    // 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);
        }
    }

    
    

    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-



    Last Updated : 21 Sep, 2018
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads