📜  Java中的队列poll()方法

📅  最后修改于: 2022-05-13 01:55:01.745000             🧑  作者: Mango

Java中的队列poll()方法

队列接口poll()方法返回并移除容器前端的元素。它删除容器中的元素。当 Queue 为空时,该方法不会抛出异常,而是返回null

句法:

E poll()

Returns:此方法返回位于容器最前面或队列头部的元素。当 Queue 为空时返回null

下面的程序说明了 Queue 的 poll() 方法:

程序 1:LinkedList的帮助下。

// Java Program Demonstrate poll()
// method of Queue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new LinkedList();
  
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.poll());
    }
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

方案二:当队列为空时,演示队列的 poll() 方法

// Java Program Demonstrate poll()
// method of Queue when the Queue becomes empty
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new LinkedList();
  
        // Add numbers to end of Queue
        Q.add(423);
        Q.add(3432);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print null as Queue is empty now
        System.out.println("Queue's head: " + Q.poll());
    }
}
输出:
Queue: [423, 3432]
Queue's head: 423
Queue's head: 3432
Queue: []
Queue's head: null

程序 3:ArrayDeque的帮助下。

// Java Program Demonstrate poll()
// method of Queue
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new ArrayDeque();
  
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.poll());
    }
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

程序 4:ConcurrentLinkedDeque的帮助下。

// Java Program Demonstrate poll()
// method of Queue
  
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new ConcurrentLinkedDeque();
  
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.poll());
    }
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

程序 5:LinkedBlockingDeque的帮助下。

// Java Program Demonstrate poll()
// method of Queue
  
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Queue
        Queue Q
            = new LinkedBlockingDeque();
  
        // Add numbers to end of Queue
        Q.add(7855642);
        Q.add(35658786);
        Q.add(5278367);
        Q.add(74381793);
  
        // print queue
        System.out.println("Queue: " + Q);
  
        // print head and deletes the head
        System.out.println("Queue's head: " + Q.poll());
  
        // print head and deleted the head
        System.out.println("Queue's head: " + Q.poll());
    }
}
输出:
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue's head: 35658786

参考: https: Java/util/Queue.html#poll–