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 NavigableSetheadSet(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:
// 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); } } |
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.
// 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); } } |
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 NavigableSetheadSet(E toElement, boolean inclusive)
Parameters: The function accept the following parameters
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:
// 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); } } |
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-
Recommended Posts:
- NavigableSet headSet() method in Java
- SortedSet headSet() method in Java
- TreeSet headSet() Method in Java
- ConcurrentSkipListSet add() method in Java
- ConcurrentSkipListSet last() method in Java
- ConcurrentSkipListSet contains() method in Java
- ConcurrentSkipListSet first() method in Java
- ConcurrentSkipListSet descendingSet() method in Java
- ConcurrentSkipListSet size() method in Java
- ConcurrentSkipListSet higher() method in Java
- ConcurrentSkipListSet equals() method in Java
- ConcurrentSkipListSet isEmpty() method in Java
- ConcurrentSkipListSet remove() method in Java
- ConcurrentSkipListSet spliterator() method in Java
- ConcurrentSkipListSet clear() method in Java
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.



