队列接口的poll()方法返回并移除容器前面的元素。它删除容器中的元素。当Queue为空时,该方法不会引发异常,而是返回null。
用法:
E poll()
返回值:此方法返回位于容器前面或队列开头的元素。当队列为空时,它返回null。
以下示例程序旨在说明队列的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<Integer> Q
= new LinkedList<Integer>();
// 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
示例2:在队列为空时演示队列的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<Integer> Q
= new LinkedList<Integer>();
// 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<Integer> Q
= new ArrayDeque<Integer>();
// 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<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
// 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<Integer> Q
= new LinkedBlockingDeque<Integer>();
// 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://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#poll–
相关用法
- Java PriorityQueue poll()用法及代码示例
- Java LinkedTransferQueue poll()用法及代码示例
- Java ConcurrentLinkedQueue poll()用法及代码示例
- Java LinkedBlockingDeque poll()用法及代码示例
- Java PriorityBlockingQueue poll()用法及代码示例
- Java LinkedBlockingQueue poll()用法及代码示例
- Java ConcurrentLinkedDeque poll()用法及代码示例
- Java ArrayBlockingQueue poll()用法及代码示例
- Java ArrayDeque poll()用法及代码示例
- Java BlockingQueue poll()用法及代码示例
- Java DelayQueue poll()用法及代码示例
- Java BlockingDeque poll()用法及代码示例
- Java Queue add()用法及代码示例
- Java Queue remove()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Queue poll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。