java.util.PriorityQueue.peek() 方法



說明

**peek()** 方法用於檢索此佇列的隊首。但不會刪除。

宣告

以下是 java.util.PriorityQueue.peek() 方法的宣告。

public E peek()							 

E peek()

引數

不適用

  • 返回值

方法呼叫將返回此佇列的隊首,如果此佇列為空,則返回 null。

引數

異常

示例

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.peek();

      System.out.println("Head of the queue is: "+ head);
   }
}

現場演示

Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3
讓我們編譯並執行以上程式,將產生以下結果。
列印頁面
廣告
© . All rights reserved.