Skip to content
Related Articles

Related Articles

Improve Article

ConcurrentLinkedQueue poll() method in Java

  • Last Updated : 26 Nov, 2018
Geek Week

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);
    }
}
Output:
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);
    }
}
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–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :