Skip to content
Related Articles

Related Articles

Improve Article

TreeSet headSet() Method in Java

  • Last Updated : 19 Jun, 2018

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:






// 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() + " ");
        }
    }
}
Output:
The resultant values till head set: 
1 
2 
3 
4 
5 
10 
20

Program 2: In an unsorted treeset:




// 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() + " ");
        }
    }
}
Output:
The resultant values till head set: 
2 
9 
10 
15 
16 
20

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




// 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() + " ");
        }
    }
}
Output:
The resultant values till head set: 
4 
Geek 
Geeks

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :