TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class. This means that the elements stored in a TreeSet are in an ordered way, i.e. in the ascending order. Because of this feature of TreeSet, it provides certain amazing methods of NavigableSet interface apart from those traditionally provided by the Collection interface.
Note: Since TreeSet is the implementation of Set, therefore it does not allow duplication of elements.
Some of specific methods provided by TreeSet are as follows:
- floor() Method
- lower() Method
- ceiling() Method
- higher() Method
- subSet() Method
- headSet() Method
- tailSet() Method
Now let us discuss all methods along with their implementation which is as follows
Method 1: floor() Method
This method returns the greatest element in the set less than or equal to the given element, or null if there is no such element.
Arguments: A number less than or equal value needs to be searched for
Syntax:
treeSetObject.floor(argument) ;
Example:
Java
// Java Program to demonstrate Floor Method in TreeSet // Importing input output classes import java.io.*; // Importing TreeSet and Set classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "Tree set = " + treeSet); // Case 1 // Using floor() method over treeSet elements but // note floor() method is inclusive of the limit // Hence maximum element is returned System.out.println( "floor(60) = " + treeSet.floor( 60 )); // Case 2 // Using floor() method over treeSet elements, // therefore output of the below line will be the // number itself System.out.println( "floor(54) = " + treeSet.floor( 54 )); } } |
Tree set = [12, 37, 54, 68, 98] floor(60) = 54 floor(54) = 54
Method 2: lower() Method
This method returns the greatest element in this set strictly less than the given element, or null if there is no such element.
Argument: Number whose less than value needs to be found.
Syntax:
treeSetObject.lower(argument) ;
Example:
Java
// Java Program to demonstrate Lower Method in TreeSet // Importing input output classes import java.io.*; // Importing TreeSet and Set classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println(treeSet); // Case 1 // Using lower() method over treeSet elements where // argument passed is greater then max element in // TreeSet therefore returning max element itself System.out.println( "lower(90) = " + treeSet.lower( 90 )); // Case 2 // Using lower() method over treeSet elements where // argument passed is not greater then max element // in TreeSet therefore returning element lesser // than that passed as an argument System.out.println( "lower(68) = " + treeSet.lower( 68 )); // Also note that lower() method is exclusive of the // limit } } |
[12, 37, 54, 68, 98] lower(90) = 68 lower(68) = 54
Method 3: ceiling() Method
This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.
Argument: Number whose less than or equal value needs to be found.
Syntax:
treeSetObject.ceiling(argument) ;
Example:
Java
// Java Program to demonstrate Ceiling Method in TreeSet // Importing input output classes import java.io.*; // Importing TreeSet and Set classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "tree set = " + treeSet); // Using ceiling() method. System.out.println( "ceiling(50) = " + treeSet.ceiling( 50 )); // Note that ceiling method is inclusive of the // limit. Therefore,output of the below line will be // the same number. System.out.println( "ceiling(68) = " + treeSet.ceiling( 68 )); // Verification for null System.out.println( "ceiling(100) = " + treeSet.ceiling( 100 )); } } |
tree set = [12, 37, 54, 68, 98] ceiling(50) = 54 ceiling(68) = 68 ceiling(100) = null
Method 4: higher() Method
This method returns the least element in this set strictly greater than the given element, or null if there is no such element.
Argument: The number whose less than value needs to be found.
Syntax:
treeSetObject.higher(argument) ;
Example:
Java
// Java Program to demonstrate Higher Method of TreeSet // Importing input output classes import java.io.*; // Importing Set and TreeSet classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "tree set = " + treeSet); // Using higher() method // Case 1 System.out.println( "higher(50) = " + treeSet.higher( 50 )); // Case 2 // Note that higher method is exclusive of the limit // causing // output be the number greater than the number // passed System.out.println( "higher(68) = " + treeSet.higher( 68 )); // Case 3 // Verification for null System.out.println( "higher(98) = " + treeSet.higher( 100 )); } } |
tree set = [12, 37, 54, 68, 98] higher(50) = 54 higher(68) = 98 higher(98) = null
Method 5: subSet() Method
This method will return elements ranging from ‘fromElement‘ to ‘toElement‘.
Note: ‘fromElement’ is inclusive and ‘toElement’ is exclusive.
Syntax:
treeSetObject.subSet(fromElement, toElement); // where fromElement is the lower limit (inclusive) and // toElement is the upper limit (exclusive) // of the set containing values between these limits
Example:
Java
// Java Program to demonstrate subset Method in TreeSet // Importing input output classes import java.io.*; // Importing Set and TreeSet classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "tree set = " + treeSet); // Using subSet() method // Case 1 System.out.println( "Elements between 54 and 98 are : " + treeSet.subSet( 54 , 98 )); // Case 2 // If we want to include the upper limit System.out.println( "Elements between 54 and 98 including both the limits : " + treeSet.subSet( 54 , true , 98 , true )); // Case 3 // If we want to exclude the lower limit System.out.println( "Elements between 54 (exclusive) and 98 (inclusive) : " + treeSet.subSet( 54 , false , 98 , true )); // Case 4 // If we want to exclude both the limits System.out.println( "Elements between 54 (exclusive) and 98 (exclusive) : " + treeSet.subSet( 54 , false , 98 , false )); } } |
tree set = [12, 37, 54, 68, 98] Elements between 54 and 98 are : [54, 68] Elements between 54 and 98 including both the limits : [54, 68, 98] Elements between 54 (exclusive) and 98 (inclusive) : [68, 98] Elements between 54 (exclusive) and 98 (exclusive) : [68]
Method 6: headSet() Method
This method will return elements of TreeSet which are less than the specified element.
Syntax:
treeSetObject.headSet(upToNumber) ;
Note: ‘upToNumber‘ is the upper limit of the numbers to be found (exclusive)
Example:
Java
// Java Program to demonstrate headSet Method in TreeSet // Importing input output classes import java.io.*; // Importing Set and TreeSet classes from java.util package import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "tree set = " + treeSet); // Implementing headSet() method via TreeSet object // Case 1 System.out.println( "Elements upto 90 : " + treeSet.headSet( 90 )); // Case 2 // The function limit is exclusive of the argument // passed System.out.println( "Elements upto 68 : " + treeSet.headSet( 68 )); // Case 3 // If we want to include the number passed, then System.out.println( "Elements upto 68 (inclusive) : " + treeSet.headSet( 68 , true )); } } |
tree set = [12, 37, 54, 68, 98] Elements upto 90 : [12, 37, 54, 68] Elements upto 68 : [12, 37, 54] Elements upto 68 (inclusive) : [12, 37, 54, 68]
Method 7:
This method will return elements of TreeSet which are greater than or equal to the specified element.
Syntax:
treeSetObject.tailSet(aboveThisNumber) ;
Note: ‘aboveThisNumber’ is the lower limit of the numbers to be found (inclusive).
Example:
Java
// Java Program to demonstrate tailSet Method in TreeSet // Importing input output classes import java.io.*; import java.util.Set; import java.util.TreeSet; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of TreeSet of Integer type // Initializing object with integer values using // Set.of() method TreeSet<Integer> treeSet = new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 )); // Print and display elements in object System.out.println( "tree set = " + treeSet); // Implementing tailSet() // Case 1 System.out.println( "Elements above 50 : " + treeSet.tailSet( 50 )); // Case 2 // The function limit is inclusive of the argument // passed System.out.println( "Elements above 54 : " + treeSet.tailSet( 54 )); // Case 3 // If we want to exclude the number passed System.out.println( "Elements above 54 (exclusive) : " + treeSet.tailSet( 54 , false )); } } |
tree set = [12, 37, 54, 68, 98] Elements above 50 : [54, 68, 98] Elements above 54 : [54, 68, 98] Elements above 54 (exclusive) : [68, 98]