SortedSet headSet() method in Java Last Updated : 30 Sep, 2018 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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. Exceptions: 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: Program 1: Java // A Java program to demonstrate // working of SortedSet import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { // Create a TreeSet and inserting elements SortedSet<Integer> s = new TreeSet<>(); // Adding Element to SortedSet s.add(1); s.add(5); s.add(2); s.add(3); s.add(9); // Returning the set with elements // strictly less than the passed value System.out.print("Elements strictly less than 7 in set are : " + s.headSet(7)); } } Output: Elements strictly less than 7 in set are : [1, 2, 3, 5] Program 2: Java // A Java program to demonstrate // working of SortedSet import java.util.SortedSet; import java.util.TreeSet; public class Main { public static void main(String[] args) { // Create a TreeSet and inserting elements SortedSet<String> s = new TreeSet<>(); // Adding Element to SortedSet s.add("Geeks"); s.add("For"); s.add("Geeks"); s.add("Code"); s.add("It"); // Returning the set with elements // strictly less than the passed value System.out.print("Element strictly less than Z in set is : " + s.headSet("Z")); } } Output: Element strictly less than Z in set is : [Code, For, Geeks, It] Reference: https://docs.oracle.com/javase/10/docs/api/java/util/SortedSet.html#headSet(E) Comment More infoAdvertise with us Next Article SortedSet headSet() method in Java B barykrg Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads SortedSet Interface in Java with Examples The SortedSet interface is present in java.util package extends the Set interface present in the collection framework. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Set interface and adds a feature that stores all the elements in this 8 min read SortedSet add() method in Java with Examples The add() method of SortedSet in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Syntax: boolean add(E element) Wh 2 min read SortedSet addAll() method in Java with Examples The addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be ad 2 min read SortedSet clear() method in Java with Examples The clear() method is used to remove all the elements from a SortedSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The method doe 1 min read TreeSet comparator() Method in Java with Examples TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equals if it is to co 3 min read SortedSet contains() method in Java with Examples The contains() method is used to check whether a specific element is present in the SortedSet or not. So basically it is used to check if a SortedSet contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of SortedSet. This is the e 2 min read SortedSet containsAll() method in Java with Examples The containsAll() method of Java SortedSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C 2 min read SortedSet first() method in Java The first() method of SortedSet interface in Java is used toReturns the first i.e., the lowest element currently in this set.Syntax: E first() Where, E is the type of element maintained by this Set.Parameters: This function does not accepts any parameter.Return Value: It returns the first or the low 2 min read SortedSet hashCode() method in Java with Examples The hashCode() method of SortedSet in Java is used to get the hashCode value for this instance of the SortedSet. It returns an integer value which is the hashCode value for this instance of the SortedSet. Syntax: public int hashCode() Parameters: This function has no parameters. Returns: The method 2 min read SortedSet headSet() method in Java 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 2 min read Like