📌  相关文章
📜  Java中的 ConcurrentLinkedQueue peek() 方法

📅  最后修改于: 2022-05-13 01:55:30.626000             🧑  作者: Mango

Java中的 ConcurrentLinkedQueue peek() 方法

ConcurrentLinkedQueuepeek()方法用于返回 ConcurrentLinkedQueue 的头部。它检索但不删除此 ConcurrentLinkedQueue 的头部。如果 ConcurrentLinkedQueue 为空,则此方法返回 null。

句法:

public E peek()

返回:此方法返回此 ConcurrentLinkedQueue 的头部而不删除它。

下面的程序说明了 ConcurrentLinkedQueue 的 peek() 方法:

示例 1:

// Java Program Demonstrate peek()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue
            queue = new ConcurrentLinkedQueue();
  
        // Add Numbers to queue
        queue.add(4353);
        queue.add(7824);
        queue.add(78249);
        queue.add(8724);
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // find peek
        int response1 = queue.peek();
  
        // print after applying peek method
        System.out.println("Head: " + response1);
  
        // Verifying that the head is not removed
        System.out.println("ConcurrentLinkedQueue after peek: " + queue);
    }
}
输出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Head: 4353
ConcurrentLinkedQueue after peek: [4353, 7824, 78249, 8724]

示例 2:

// Java Program Demonstrate peek()
// method of ConcurrentLinkedQueue
  
import java.util.concurrent.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ConcurrentLinkedQueue
        ConcurrentLinkedQueue
            queue = new ConcurrentLinkedQueue();
  
        // Add String to queue
        queue.add("Aman");
        queue.add("Amar");
        queue.add("Sanjeet");
        queue.add("Rabi");
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("ConcurrentLinkedQueue: " + queue);
  
        // find peek of queue
        String response1 = queue.peek();
  
        // print after applying peek method
        System.out.println("Head: " + response1);
  
        // Verifying that the head is not removed
        System.out.println("ConcurrentLinkedQueue after peek: " + queue);
  
        // remove some elements
        queue.poll();
        queue.poll();
  
        // Displaying the existing ConcurrentLinkedQueue
        System.out.println("Updated ConcurrentLinkedQueue: " + queue);
  
        // find peek of queue
        String response2 = queue.peek();
  
        // print after applying peek method
        System.out.println("Head: " + response1);
  
        // Verifying that the head is not removed
        System.out.println("ConcurrentLinkedQueue after peek: " + queue);
    }
}
输出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]

Head: Aman

ConcurrentLinkedQueue after peek: [Aman, Amar, Sanjeet, Rabi]

Updated ConcurrentLinkedQueue: [Sanjeet, Rabi]

Head: Aman

ConcurrentLinkedQueue after peek: [Sanjeet, Rabi]

参考: https: Java/util/concurrent/ConcurrentLinkedQueue.html#peek–