ConcurrentLinkedQueue poll() method in Java
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:
// Java Program Demonstrate poll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); // Add Numbers to queue queue.add(4353); queue.add(7824); queue.add(78249); queue.add(8724); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // apply poll() int response1 = queue.poll(); // print after applying poll method System.out.println("Head: " + response1); // Displaying the existing ConcurrentLinkedQueue System.out.println("Current ConcurrentLinkedQueue: " + queue); } } |
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Head: 4353 Current ConcurrentLinkedQueue: [7824, 78249, 8724]
Example 2:
// Java Program Demonstrate poll() // method of ConcurrentLinkedQueue import java.util.concurrent.*; public class GFG { public static void main(String[] args) { // create an ConcurrentLinkedQueue ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); // Add String to queue queue.add("Aman"); queue.add("Amar"); queue.add("Sanjeet"); queue.add("Rabi"); // Displaying the existing ConcurrentLinkedQueue System.out.println("ConcurrentLinkedQueue: " + queue); // apply poll() on queue String response1 = queue.poll(); // print after applying poll method System.out.println("Head: " + response1); // Displaying the existing ConcurrentLinkedQueue System.out.println("Current ConcurrentLinkedQueue: " + queue); // apply poll() on queue more than one time queue.poll(); queue.poll(); // Displaying the existing ConcurrentLinkedQueue System.out.println("After 2 poll() applied\n" + "ConcurrentLinkedQueue: " + queue); } } |
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–
Recommended Posts:
- ConcurrentLinkedQueue add() method in Java
- ConcurrentLinkedQueue contains() method in Java
- ConcurrentLinkedQueue addAll() method in Java
- ConcurrentLinkedQueue offer() method in Java
- ConcurrentLinkedQueue iterator() method in Java
- ConcurrentLinkedQueue peek() method in Java
- ConcurrentLinkedQueue remove() method in Java
- ConcurrentLinkedQueue spliterator() method in Java
- ConcurrentLinkedQueue size() method in Java
- ConcurrentLinkedQueue isEmpty() method in Java
- ConcurrentLinkedQueue toArray() Method in Java
- LinkedTransferQueue poll() method in Java
- PriorityQueue poll() Method in Java
- PriorityBlockingQueue poll() method in Java
- Queue poll() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



