ArrayBlockingQueue peek() 方法在 Java 中


ArrayBlockingQueue 類的 peek() 方法返回此佇列的頭部,如果該佇列為空則返回 null。

語法如下。

public E peek()

要使用 ArrayBlockingQueue 類,你需要匯入下列包。

import java.util.concurrent.ArrayBlockingQueue;

以下是如何在 Java ArrayBlockingQueue 類中實現 peek() 方法的示例。

示例

 即時演示

import java.util.concurrent.ArrayBlockingQueue;
public class Demo {
   public static void main(String[] args) throws InterruptedException {
      ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10);
      q.add(200);
      q.add(310);
      q.add(400);
      q.add(450);
      q.add(500);
      q.add(550);
      q.add(700);
      System.out.println("ArrayBlockingQueue = " + q);
      System.out.println("Head of the queue = "+q.peek());
   }
}

輸出

ArrayBlockingQueue = [200, 310, 400, 450, 500, 550, 700]
Head of the queue = 200

更新於: 30-07-2019

105 次檢視

Kickstart Your Career

透過完成課程獲得認證

開始
廣告