TreeSet subSet() Method in Java

The java.util.TreeSet.subSet() is used to return a subset of the existing TreeSet within a range mentioned in the parameter. The method takes in an upper limit and a lower limit and returns all the elements mentioned in the range. The lower limit is included if the element is present within the set and the upper limit is excluded. Basically, it takes the subset greater than equal to the lower limit and strictly less than the upper element.

Syntax:

TreeSet tree_set.subSet(Object low_element, Object up_element)

Parameter:

  • low_element: This is of the type of the TreeSet and defines the lower limit or the starting element from which the subset is evaluated. This element is included in the subset.
  • up_element: This is of the type of the TreeSet and defines the upper limit or the last element up to which the subset is evaluated. This element is excluded from the subset.

Return Values: The method returns a subset of the TreeSet type mentioned within the given range of the parameters.

Below programs illustrate the java.util.TreeSet.subSet() method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate subSet() 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(5);
        tree_set.add(1);
        tree_set.add(50);
        tree_set.add(10);
        tree_set.add(20);
        tree_set.add(6);
        tree_set.add(20);
        tree_set.add(18);
        tree_set.add(9);
        tree_set.add(30);
  
        // Creating the subset tree
        TreeSet<Integer> sub_set = new TreeSet<Integer>();
  
        // Limiting the values till 5
        sub_set = (TreeSet<Integer>)tree_set.subSet(6, 30);
  
        // Creating an Iterator
        Iterator iterate;
        iterate = sub_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values within the sub set: ");
  
        // Iterating through the subset
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

chevron_right


Output:

The resultant values within the sub set: 
6 
9 
10 
18 
20

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate subSet() method when TreeSet
// contains elements of String type
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 subset tree
        TreeSet<String> sub_set = new TreeSet<String>();
  
        // Limiting the values till 5
        sub_set = (TreeSet<String>)tree_set.subSet("4", "TreeSet");
  
        // Creating an Iterator
        Iterator iterate;
        iterate = sub_set.iterator();
  
        // Displaying the tree set data
        System.out.println("The resultant values within the sub set: ");
  
        // Iterating through the subset
        while (iterate.hasNext()) {
            System.out.println(iterate.next() + " ");
        }
    }
}

chevron_right


Output:

The resultant values within the sub set: 
4 
Geek 
Geeks 
To


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.