ConcurrentSkipListSet pollFirst() method in Java
Last Updated :
21 Sep, 2018
The pollFirst() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns retrieves and removes the first (lowest) element, or returns null if this set is empty.
Syntax:
public E pollFirst()
Return Value: The function returns retrieves and removes the first (lowest) element, or returns null if this set is empty.
Below programs illustrate the ConcurrentSkipListSet.pollFirst() method:
Program 1:
import java.util.concurrent.*;
class ConcurrentSkipListSetpollFirstExample1 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
Lset = new ConcurrentSkipListSet<Integer>();
for ( int i = 10 ; i <= 50 ; i += 10 )
Lset.add(i);
System.out.println( "Contents of the set: " + Lset);
System.out.println( "The first element of the set: "
+ Lset.pollFirst());
System.out.println( "Contents of the set after pollFirst: "
+ Lset);
}
}
|
Output:
Contents of the set: [10, 20, 30, 40, 50]
The first element of the set: 10
Contents of the set after pollFirst: [20, 30, 40, 50]
Program 2:
import java.util.concurrent.*;
class ConcurrentSkipListSetpollFirstExample2 {
public static void main(String[] args)
{
ConcurrentSkipListSet<Integer>
Lset = new ConcurrentSkipListSet<Integer>();
System.out.println( "Contents of the set: " + Lset);
System.out.println( "The first element of the set: "
+ Lset.pollFirst());
}
}
|
Output:
Contents of the set: []
The first element of the set: null
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#pollFirst–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...