TreeSet headSet() Method in Java With Examples Last Updated : 01 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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 correctly implement the Set interface. The headSet() method of TreeSet class been present inside java.util package is used as a limit setter for a tree set, to return the elements up to a limit defined in the parameter of the method in a sorted manner excluding the element. Syntax: head_set = (TreeSet)tree_set.headSet(Object element) Parameters: The parameter element is of the type of the tree set and is the headpoint that is the limit up to which the tree is allowed to return values excluding the element itself. Return Values: The method returns the portion of the values in a sorted manner that is strictly less than the element mentioned in the parameter. now we will be discussing different scenarios while implementing the headSet() method in TreeSet class: Case 1: In a sorted TreeSetCase 2-A: In an unsorted TreeSetCase 2-B: In an unsorted TreeSet but with String type elements Example 1: Java // Java program to Illustrate headSet() method // of TreeSet class In a sorted TreeSet // Importing required classes import java.io.*; import java.util.Iterator; import java.util.TreeSet; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeSet by // declaring object of TreeSet class TreeSet<Integer> tree_set = new TreeSet<Integer>(); // Adding the elements // using add() method tree_set.add(1); tree_set.add(2); tree_set.add(3); tree_set.add(4); tree_set.add(5); tree_set.add(10); tree_set.add(20); tree_set.add(30); tree_set.add(40); tree_set.add(50); // Creating the headSet tree TreeSet<Integer> head_set = new TreeSet<Integer>(); // Limiting the values till 5 head_set = (TreeSet<Integer>)tree_set.headSet(30); // Creating an Iterator Iterator iterate; iterate = head_set.iterator(); // Displaying the tree set data System.out.println( "The resultant values till head set: "); // Holds true till there is single element // remaining in the object while (iterate.hasNext()) { // Iterating through the headSet // using next() method System.out.println(iterate.next() + " "); } } } Output: The resultant values till head set: 1 2 3 4 5 10 20 Example 2-A: Java // Java Program to illustrate headSet() method // of TreeSet class In an unsorted TreeSet // Importing required classes import java.io.*; import java.util.Iterator; import java.util.TreeSet; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeSet TreeSet<Integer> tree_set = new TreeSet<Integer>(); // Adding the elements // using add() method tree_set.add(9); tree_set.add(2); tree_set.add(100); tree_set.add(40); tree_set.add(50); tree_set.add(10); tree_set.add(20); tree_set.add(30); tree_set.add(15); tree_set.add(16); // Creating the headSet tree TreeSet<Integer> head_set = new TreeSet<Integer>(); // Limiting the values till 5 head_set = (TreeSet<Integer>)tree_set.headSet(30); // Creating an Iterator Iterator iterate; iterate = head_set.iterator(); // Displaying the tree set data System.out.println("The resultant values till head set: "); // Iterating through the headSet while (iterate.hasNext()) { // Printing the elements System.out.println(iterate.next() + " "); } } } Output: The resultant values till head set: 2 9 10 15 16 20 Example 2-B: Java // Java code to illustrate headSet() method of TreeSet class // In an unsorted treeset but with String type elements // Importing required classes import java.io.*; import java.util.Iterator; import java.util.TreeSet; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an empty TreeSet TreeSet<String> tree_set = new TreeSet<String>(); // Adding the elements using add() tree_set.add("Welcome"); tree_set.add("To"); tree_set.add("Geek"); tree_set.add("4"); tree_set.add("Geeks"); tree_set.add("TreeSet"); // Creating the headSet tree TreeSet<String> head_set = new TreeSet<String>(); // Limiting the values till 5 head_set = (TreeSet<String>)tree_set.headSet("To"); // Creating an Iterator Iterator iterate; iterate = head_set.iterator(); // Displaying the tree set data System.out.println( "The resultant values till head set: "); // Iterating through the headSet while (iterate.hasNext()) { // Printing elements using next() method System.out.println(iterate.next() + " "); } } } Output: The resultant values till head set: 4 Geek Geeks Comment More infoAdvertise with us Next Article TreeSet headSet() Method in Java With Examples C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package java-treeset +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black 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 equ 13 min read TreeSet add() Method in Java The Java.util.TreeSet.add() method in Java TreeSet is used to add a specific element into a TreeSet. The function adds the element only if the specified element is not already present in the set else the function return False if the element is not present in the TreeSet. Syntax: Tree_Set.add(Object 1 min read TreeSet addAll() Method in Java The java.util.TreeSet.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 typ 2 min read TreeSet ceiling() method in Java with Examples The ceiling() method of java.util.TreeSet<E> class is used to return the least element in this set greater than or equal to the given element, or null if there is no such element. Syntax: public E ceiling(E e) Parameters: This method takes the value e as a parameter which is to be matched. Ret 2 min read TreeSet clear() Method in Java The Java.util.TreeSet.clear() method is used to remove all the elements from a TreeSet. 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 TreeSet. Syntax: Tree_Set.clear() Pa 1 min read TreeSet clone() Method in Java The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates t 1 min read TreeSet contains() 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 TreeSet descendingIterator() method in Java with Examples The descendingIterator() method of java.util.TreeSet<E> class is used to return an iterator over the elements in this set in descending order.Syntax: public Iterator descendingIterator()Return Value: This method returns an iterator over the elements in this set in descending order.Below are th 2 min read TreeSet descendingSet() method in Java with Examples The descendingSet() method of java.util.TreeSet<E> class is used to return a reverse order view of the elements contained in this set. The descending set is backed by this set, so changes to the set are reflected in the descending set, and vice-versa. If either set is modified while an iterati 2 min read TreeSet first() Method in Java The Java.util.TreeSet.first() method is used to return the first of the element of a TreeSet. The first element here is being referred to the lowest of the elements in the set. If the elements are of integer types then the smallest integer is returned. If the elements are of the string types then th 3 min read Like