Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaConcurrentSkipListSet tailSet() method in Java with Examples

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, boolean inclusive)

Parameters: 
The first variation of this method takes only one parameter, i.e. the fromElement E from where the elements which are greater than or equal to this element are returned from the set.
The second variation is similar to the first one but here the second parameter is boolean inclusive if it is set to false 
then the element E (if present in the list) will not be included.
Returns: 
This method returns a view of the portion of this set whose elements are greater than or equal to fromElement. 
In the case of the second variation, the inclusion of this fromElement is decided by boolean type.
Exception: 
Null Pointer Exception: if the specified element is NULL.
Below are the sample programs to illustrate ConcurrentSkipListSet tailSet() in Java:
Example: 1 
The elements which are greater than 200 are returned.
 

Java




// Java program to demonstrate ConcurrentSkipListSet tailSet() method
 
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetLastExample1 {
    public static void main(String[] args)
    {
 
        // Initializing the set using ConcurrentSkipListSet()
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
 
        // Adding elements to this set
        set.add(199);
        set.add(256);
        set.add(958);
        set.add(421);
        set.add(80);
 
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
 
        // Printing the elements of ConcurrentSkipListSet that
        // are returned by tailSet() method
        System.out.println("The returned elements are: "
                           + set.tailSet(200));
    }
}


Output: 

ConcurrentSkipListSet: [80, 199, 256, 421, 958]
The returned elements are: [256, 421, 958]

 

Example: 2 
In this example, the element 35 is not returned because boolean inclusive is false. 
 

Java




// Java program to demonstrate ConcurrentSkipListSet tailSet() method
 
import java.util.concurrent.ConcurrentSkipListSet;
 
class ConcurrentSkipListSetLastExample1 {
    public static void main(String[] args)
    {
 
        // Initializing the set using ConcurrentSkipListSet()
        ConcurrentSkipListSet<Integer>
            set = new ConcurrentSkipListSet<Integer>();
 
        // Adding elements to this set
        set.add(13);
        set.add(35);
        set.add(9);
        set.add(41);
        set.add(90);
 
        // Printing the ConcurrentSkipListSet
        System.out.println("ConcurrentSkipListSet: "
                           + set);
 
        // Printing the elements of ConcurrentSkipListSet that
        // are returned by tailSet() method
        System.out.println("The returned elements are: "
                           + set.tailSet(35, false));
    }
}


Output: 

ConcurrentSkipListSet: [9, 13, 35, 41, 90]
The returned elements are: [41, 90]

 

RELATED ARTICLES

Most Popular

Recent Comments