ConcurrentSkipListSet headSet() method in Java

headSet(E toElement)

The java.util.concurrent.ConcurrentSkipListSet.headSet() method is an in-built function in Java which returns a view of the portion of this set whose elements are strictly less than toElement. 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 headSet(E toElement)

Parameters: The function accepts a single parameter toElement i.e. high endpoint of the returned set.



Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements are strictly less than toElement.

Below programs illustrate the ConcurrentSkipListSet.headSet(E toElement) method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample1 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Creating a headSet object with upper limit 30
        NavigableSet<Integer> hd_set = set.headSet(30);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        // Printing the elements of the headSet set
        System.out.println("Contents of the headset"
                           + " with upper limit 30: "
                           + hd_set);
    }
}

chevron_right


Output:

Contents of the set: [10, 20, 30, 40, 50]
Contents of the headset with upper limit 30: [10, 20]

Program 2: Program to show NullPointerException.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate headSet()
// method of ConcurrentSkipListSet */
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentSkipListSet;
  
class ConcurrentSkipListSetHeadSetExample2 {
    public static void main(String[] args)
    {
  
        // Initializing the set
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
  
        // Adding elements to this set
        for (int i = 10; i <= 50; i += 10)
            set.add(i);
  
        // Printing the elements of the set
        System.out.println("Contents of the set: " + set);
  
        try {
            // Trying to creating a headSet object with upper limit null
            NavigableSet<Integer> hd_set = set.headSet(null);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
        // Printing the elements of the headSet set
        // System.out.println("Contents of the headset"
+" with upper limit 30: "+ hd_set);
    }
}

chevron_right


Output:

Contents of the set: [10, 20, 30, 40, 50]
Exception: java.lang.NullPointerException

headSet(E toElement, boolean inclusive)

The java.util.concurrent.ConcurrentSkipListSet.headSet() method is an in-built function in Java which returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement. 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 headSet(E toElement,
                      boolean inclusive)

Parameters: The function accept the following parameters

  • toElement i.e. high endpoint of the returned set.
  • inclusive – true if the high endpoint is to be included in the returned view.
  • Return Value: The function returns a NavigableSet which is a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.

    Below programs illustrate the ConcurrentSkipListSet.headSet(E toElement, boolean inclusive) method:

    Program 3:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program Demonstrate headSet()
    // method of ConcurrentSkipListSet */
      
    import java.util.NavigableSet;
    import java.util.concurrent.ConcurrentSkipListSet;
      
    class ConcurrentSkipListSetHeadSetExample3 {
        public static void main(String[] args)
        {
      
            // Initializing the set
            ConcurrentSkipListSet<Integer>
                set = new ConcurrentSkipListSet<Integer>();
      
            // Adding elements to this set
            for (int i = 10; i <= 50; i += 10)
                set.add(i);
      
            // Creating a headSet object with upper limit 30 inclusive
            NavigableSet<Integer> hd_set = set.headSet(30, true);
      
            // Printing the elements of the set
            System.out.println("Contents of the set: " + set);
      
            // Printing the elements of the headSet set
            System.out.println("Contents of the headset"
                               + " with upper limit 30 inclusive: "
                               + hd_set);
        }
    }

    chevron_right

    
    

    Output:

    Contents of the set: [10, 20, 30, 40, 50]
    Contents of the headset with upper limit 30 inclusive: [10, 20, 30]
    

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



    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.