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 TreeSet
- Case 2-A: In an unsorted TreeSet
- Case 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() + " " ); } } } |
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() + " " ); } } } |
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() + " " ); } } } |
The resultant values till head set: 4 Geek Geeks