當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.PriorityQueue.poll()用法及代碼示例


描述

這個poll()方法用於檢索和移除此隊列的頭部,如果此隊列為空,則返回 null。

聲明

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

public E poll()							 

參數

NA

返回值

  • 方法調用返回隊列的頭部,如果隊列為空,則返回 null。

異常

NA

示例

下麵的例子展示了 java.util.PriorityQueue.poll() 的用法

package com.tutorialspoint;

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]

相關用法


注:本文由純淨天空篩選整理自 java.util.PriorityQueue.poll() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。