Last Updated : 11 Jul, 2025
The tailSet() method of
SortedSet interface in Javais used to return a view of the portion of this set whose elements are greater than or equal to the parameter
fromElement.
: The set returned by this method will throw an
IllegalArgumentExceptionif an attempt is made to insert an element outside its range.
Syntax:
SortedSet tailSet(E fromElement)
Where, E is the type of element maintained by this Set.
Parameters: This function accepts a single parameter
fromElementwhich represent the low endpoint (inclusive) of the returned set.
Return Value: It returns the elements which are greater than or equal to the given argument
fromElement. Exceptions:
Below programs illustrate the above method:
Program 1:
Java
// A Java program to demonstrate
// working of SortedSet
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args)
{
// Create a TreeSet and inserting elements
SortedSet<Integer> s = new TreeSet<>();
// Adding Element to SortedSet
s.add(1);
s.add(5);
s.add(2);
s.add(3);
s.add(9);
// Returning the set with elements
// strictly less than the passed value
System.out.print("Elements greater than or equal to 5 in set are : "
+ s.tailSet(5));
}
}
Output:
Elements greater than or equal to 5 in set are : [5, 9]Program 2
:
Java
// A Java program to demonstrate
// working of SortedSet
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args)
{
// Create a TreeSet and inserting elements
SortedSet<String> s = new TreeSet<>();
// Adding Element to SortedSet
s.add("Geeks");
s.add("For");
s.add("Geeks");
s.add("Code");
s.add("It");
// Returning the set with elements
// strictly less than the passed value
System.out.print("Element greater than or equal to G in set is : "
+ s.tailSet("G"));
}
}
Output:
Element greater than or equal to G in set is : [Geeks, It]Reference
:
https://docs.oracle.com/javase/10/docs/api/java/util/SortedSet.html#tailSet(E)RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4