The headSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are strictly less than the parameter toElement.
- The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.
- The set returned by this method supports all optional set operations that this set supports.
Note: The set returned by this method will throw an IllegalArgumentException if an attempt is made to insert an element outside its range.
SortedSet headSet(E toElement)
Where, E is the type of element maintained by this Set.
Parameters: This function accepts a single parameter toElement which represent the high endpoint (exclusive) of the returned set.
Return Value: It returns the last or the highest element currently in the set.
- ClassCastException : It throws a ClassCastException if toElement is not compatible with this set’s comparator (or, if the set has no comparator, if toElement does not implement Comparable).
- NullPointerException : It throws a NullPointerException if the parameter toElement is null.
- IllegalArgumentException : It throws an IllegalArgumentException this set itself has a restricted range, and the parameter toElement lies outside the bounds of the range.
Below programs illustrate the above method:
Elements strictly less than 7 in set are : [1, 2, 3, 5]
Element strictly less than Z in set is : [Code, For, Geeks, It]
- TreeSet headSet() Method in Java
- ConcurrentSkipListSet headSet() method in Java
- NavigableSet headSet() method in Java
- SortedSet first() method in Java
- SortedSet last() method in Java
- SortedSet add() method in Java with Examples
- SortedSet subSet() method in Java
- SortedSet contains() method in Java with Examples
- SortedSet tailSet() method in Java
- SortedSet toArray() method in Java with Examples
- SortedSet retainAll() method in Java with Examples
- SortedSet equals() method in Java with Examples
- SortedSet remove() method in Java with Examples
- SortedSet size() method in Java with Examples
- SortedSet containsAll() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.