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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
public class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "Tree set = " + treeSet);
System.out.println( "floor(60) = "
+ treeSet.floor( 60 ));
System.out.println( "floor(54) = "
+ treeSet.floor( 54 ));
}
}
|
Output
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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println(treeSet);
System.out.println( "lower(90) = "
+ treeSet.lower( 90 ));
System.out.println( "lower(68) = "
+ treeSet.lower( 68 ));
}
}
|
Output
[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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "tree set = " + treeSet);
System.out.println( "ceiling(50) = "
+ treeSet.ceiling( 50 ));
System.out.println( "ceiling(68) = "
+ treeSet.ceiling( 68 ));
System.out.println( "ceiling(100) = "
+ treeSet.ceiling( 100 ));
}
}
|
Output
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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "tree set = " + treeSet);
System.out.println( "higher(50) = "
+ treeSet.higher( 50 ));
System.out.println( "higher(68) = "
+ treeSet.higher( 68 ));
System.out.println( "higher(98) = "
+ treeSet.higher( 100 ));
}
}
|
Output
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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "tree set = " + treeSet);
System.out.println(
"Elements between 54 and 98 are : "
+ treeSet.subSet( 54 , 98 ));
System.out.println(
"Elements between 54 and 98 including both the limits : "
+ treeSet.subSet( 54 , true , 98 , true ));
System.out.println(
"Elements between 54 (exclusive) and 98 (inclusive) : "
+ treeSet.subSet( 54 , false , 98 , true ));
System.out.println(
"Elements between 54 (exclusive) and 98 (exclusive) : "
+ treeSet.subSet( 54 , false , 98 , false ));
}
}
|
Output
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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "tree set = " + treeSet);
System.out.println( "Elements upto 90 : "
+ treeSet.headSet( 90 ));
System.out.println( "Elements upto 68 : "
+ treeSet.headSet( 68 ));
System.out.println( "Elements upto 68 (inclusive) : "
+ treeSet.headSet( 68 , true ));
}
}
|
Output
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
import java.io.*;
import java.util.Set;
import java.util.TreeSet;
class GFG {
public static void main(String[] args)
{
TreeSet<Integer> treeSet
= new TreeSet<>(Set.of( 12 , 98 , 54 , 37 , 68 ));
System.out.println( "tree set = " + treeSet);
System.out.println( "Elements above 50 : "
+ treeSet.tailSet( 50 ));
System.out.println( "Elements above 54 : "
+ treeSet.tailSet( 54 ));
System.out.println(
"Elements above 54 (exclusive) : "
+ treeSet.tailSet( 54 , false ));
}
}
|
Output
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]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Feb, 2023
Like Article
Save Article