Open In App
Related Articles

TreeSet tailSet() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
The java.util.TreeSet.tailSet() method is used to set a point of start for a tree set, to return all the elements greater than the element passed as parameter mentioned to the method in a sorted manner including the element(if the element is mentioned in the tree). Syntax:
TreeSet tail_set.tailSet(Object element)
Parameters: The parameter element is of the type of the TreeSet and is the start point from which the tree is allowed to return the values greater than the value mentioned in the parameter including the element. Return Values: The method returns the portion of the values in a sorted manner that is greater than the element mentioned in the parameter, including the parameter. Below programs illustrate the use of java.util.TreeSet.tailSet(): Program 1: In a sorted TreeSet.
// Java code to illustrate TreeSet.tailSet() method
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 tailSet tree
        TreeSet<Integer> tail_set = new TreeSet<Integer>();
  
        // Limiting the values till 5
        tail_set = (TreeSet<Integer>)tree_set.tailSet(10);
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

                    
Output:
The resultant values from the tail: 
10 
20 
30 
40 
50
Program 2: In an unsorted TreeSet.
// Java code to illustrate tailSet()
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 tailSet tree
        TreeSet<Integer> tail_set = new TreeSet<Integer>();
  
        // Limiting the values till 5
        tail_set = (TreeSet<Integer>)tree_set.tailSet(25);
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

                    
Output:
The resultant values from the tail: 
30 
40 
50 
100
Program 3: In an unsorted TreeSet but with String type elements.
// Java code to illustrate tailSet()
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 tailSet tree
        TreeSet<String> tail_set = new TreeSet<String>();
  
        // Limiting the values till 5
        tail_set = (TreeSet<String>)tree_set.tailSet("To");
  
        // Creating an Iterator
        Iterator iterate;
        iterate = tail_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values from the tail: ");
  
        // Iterating through the tailSet
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

                    
Output:
The resultant values from the tail: 
To 
TreeSet 
Welcome


Last Updated : 26 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Similar Reads