ConcurrentLinkedQueue的peek()方法用于返回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<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
// 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<String>
queue = new ConcurrentLinkedQueue<String>();
// 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#peek–
相关用法
- Java ConcurrentLinkedQueue contains()用法及代码示例
- Java ConcurrentLinkedQueue add()用法及代码示例
- Java ConcurrentLinkedQueue iterator()用法及代码示例
- Java ConcurrentLinkedQueue poll()用法及代码示例
- Java ConcurrentLinkedQueue addAll()用法及代码示例
- Java ConcurrentLinkedQueue isEmpty()用法及代码示例
- Java ConcurrentLinkedQueue offer()用法及代码示例
- Java ConcurrentLinkedQueue size()用法及代码示例
- Java ConcurrentLinkedQueue toArray()用法及代码示例
- Java ConcurrentLinkedQueue spliterator()用法及代码示例
- Java ConcurrentLinkedQueue remove()用法及代码示例
- Java PriorityQueue peek()用法及代码示例
- Java Queue peek()用法及代码示例
- Java LinkedBlockingDeque peek()用法及代码示例
- Java ArrayDeque peek()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ConcurrentLinkedQueue peek() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。