目录

E poll()

描述 (Description)

poll()方法用于检索和删除此队列的头部,如果此队列为空,则返回null。

声明 (Declaration)

以下是java.util.PriorityQueue.poll()方法的声明。

public E poll()							 

参数 (Parameters)

NA

返回值 (Return Value)

  • 方法调用返回队列的头部,如果队列为空,则返回null。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.util.PriorityQueue.poll()的用法

package com.iowiki;
import java.util.*;
public class PriorityQueueDemo {
   public static void main(String args[]) {
      // create priority queue
      PriorityQueue < Integer >  prq = new PriorityQueue < Integer > (); 
      // insert values in the queue
      for ( int i = 3; i  <  10; i++ ) {  
         prq.add (new Integer (i)) ; 
      }
      System.out.println("Initial priority queue values are: "+ prq);
      // get the head from the queue
      Integer head = prq.poll();
      System.out.println("Head of the queue is: "+ head);
      System.out.println("Priority queue values after poll: "+ prq);
   }
}

让我们编译并运行上面的程序,这将产生以下结果。

Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3
Priority queue values after poll: [4, 6, 5, 9, 7, 8]
↑回到顶部↑
WIKI教程 @2018