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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Head: 4353
Current ConcurrentLinkedQueue: [7824, 78249, 8724]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.