TreeSet headSet() Method in Java

The java.util.TreeSet.headSet() method 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.

Below programs illustrate the use of java.util.TreeSet.headSet():
Program 1: In a sorted treeset:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
  
public class Tree_Set_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeSet
        TreeSet<Integer> tree_set = new TreeSet<Integer>();
  
        // Adding the elements using add()
        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: ");
  
        // Iterating through the headSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

chevron_right


Output:

The resultant values till head set: 
1 
2 
3 
4 
5 
10 
20

Program 2: In an unsorted treeset:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
  
public class Tree_Set_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty TreeSet
        TreeSet<Integer> tree_set = new TreeSet<Integer>();
  
        // Adding the elements using add()
        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()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

chevron_right


Output:

The resultant values till head set: 
2 
9 
10 
15 
16 
20

Program 3: In an unsorted treeset but with String type elements:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate headSet()
import java.io.*;
import java.util.Iterator;
import java.util.TreeSet;
  
public class Tree_Set_Demo {
    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()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

chevron_right


Output:

The resultant values till head set: 
4 
Geek 
Geeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.