Open In App

SortedSet tailSet() method in Java

Last Updated : 30 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The tailSet() method of SortedSet interface in Java is 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 is backed by this set, so changes in the returned set are reflected in this set, and vice-versa.
  • The set returned by this method supports all optional set operations that this set supports.

Note: The set returned by this method will throw an IllegalArgumentException if 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 fromElement which 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:

  • ClassCastException : It throws a ClassCastException if fromElement is not compatible with this set’s comparator (or, if the set has no comparator, if fromElement does not implement Comparable).
  • NullPointerException : It throws a NullPointerException if the parameter fromElement is null.
  • IllegalArgumentException : It throws an IllegalArgumentException this set itself has a restricted range, and the parameter fromElement lies outside the bounds of the range.

Below programs illustrate the above method:

Program 1:




// 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:




// 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)



Previous Article
Next Article

Similar Reads

TreeSet tailSet() Method in Java
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 i
3 min read
ConcurrentSkipListSet tailSet() method in Java with Examples
The tailSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java where the elements which are equal to or greater than the specified element are returned. The syntax of the function gives a clear understanding of the specified element followed by the examples.Syntax: tailSet(E fromElement) or tailSet(E fromElement,
2 min read
SortedSet subSet() method in Java
The subSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The set returned by this method supports al
2 min read
SortedSet headSet() method in Java
The headSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are strictly less than the parameter toElement. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The set returned by this method supports all optional
2 min read
SortedSet first() method in Java
The first() method of SortedSet interface in Java is used toReturns the first i.e., the lowest element currently in this set.Syntax: E first() Where, E is the type of element maintained by this Set.Parameters: This function does not accepts any parameter.Return Value: It returns the first or the lowest element currently in the set.Exceptions: It th
2 min read
SortedSet last() method in Java
The last() method of SortedSet interface in Java is used to return the last i.e., the highest element currently in this set.Syntax: E last() Where, E is the type of element maintained by this Set.Parameters: This function does not accepts any parameter.Return Value: It returns the last or the highest element currently in the set.Exceptions: It thro
2 min read
SortedSet add() method in Java with Examples
The add() method of SortedSet in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Syntax: boolean add(E element) Where E is the type of element maintained by this Se
2 min read
SortedSet equals() method in Java with Examples
The equals() method of java.util.SortedSet class is used to verify the equality of an Object with a SortedSet and compare them. The method returns true if the size of both the SortedSets are equal and both contain the same elements. Syntax: public boolean equals(Object o) Parameters: This method takes the object o as a parameter to be compared for
2 min read
SortedSet addAll() method in Java with Examples
The addAll(Collection C) method is used to append all of the elements from the mentioned collection to the existing set. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the set. Return Value: The method returns t
2 min read
SortedSet isEmpty() method in Java with Examples
The java.util.SortedSet.isEmpty() method is used to check if a SortedSet is empty or not. It returns True if the SortedSet is empty otherwise it returns False. Syntax: boolean isEmpty() Parameters: This method does not take any parameter. Return Value: The method returns True if the SortedSet is empty else returns False. Note: The isEmpty() method
1 min read