ConcurrentLinkedQueue poll() method in Java
Last Updated :
26 Nov, 2018
The poll() method of ConcurrentLinkedQueue is used to remove and return the head of this ConcurrentLinkedQueue. If the ConcurrentLinkedQueue is empty then this method will return null.
Syntax:
public E poll()
Returns: This method remove and returns the head of this ConcurrentLinkedQueue or null if this queue is empty.
Below programs illustrate poll() method of ConcurrentLinkedQueue:
Example 1:
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
queue.add(4353);
queue.add(7824);
queue.add(78249);
queue.add(8724);
System.out.println("ConcurrentLinkedQueue: " + queue);
int response1 = queue.poll();
System.out.println("Head: " + response1);
System.out.println("Current ConcurrentLinkedQueue: " + queue);
}
}
|
Output:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Head: 4353
Current ConcurrentLinkedQueue: [7824, 78249, 8724]
Example 2:
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
queue.add("Aman");
queue.add("Amar");
queue.add("Sanjeet");
queue.add("Rabi");
System.out.println("ConcurrentLinkedQueue: " + queue);
String response1 = queue.poll();
System.out.println("Head: " + response1);
System.out.println("Current ConcurrentLinkedQueue: " + queue);
queue.poll();
queue.poll();
System.out.println("After 2 poll() applied\n"
+ "ConcurrentLinkedQueue: " + queue);
}
}
|
Output:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
Head: Aman
Current ConcurrentLinkedQueue: [Amar, Sanjeet, Rabi]
After 2 poll() applied
ConcurrentLinkedQueue: [Rabi]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#poll–
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...