Java 中的 LinkedBlockingQueue peek() 方法
LinkedBlockingQueue 的 peek() 方法返回 LinkedBlockingQueue 的头部。它检索 LinkedBlockingQueue 头部的值,但不删除它。如果 LinkedBlockingQueue 为空,则此方法返回 null。
语法:
public E peek()
返回值: 此方法返回 LinkedBlockingQueue 的头部。
下面的程序说明了 LinkedBlockingQueue 类的 peek() 方法:
程序1:使用 peek() 方法返回容量为 7 的 LinkedBlockingQueue 的头部,其中包含名称列表。
// Java Program Demonstrate peek()
// method of LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of LinkedBlockingQueue
int capacityOfQueue = 7;
// create object of LinkedBlockingQueue
LinkedBlockingQueue<String> linkedQueue
= new LinkedBlockingQueue<String>(capacityOfQueue);
// Add element to LinkedBlockingQueue
linkedQueue.add("John");
linkedQueue.add("Tom");
linkedQueue.add("Clark");
linkedQueue.add("Kat");
// find head of linkedQueue using peek() method
String head = linkedQueue.peek();
// print result
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
// removing one element
linkedQueue.remove();
// again get head of queue
head = linkedQueue.peek();
// print result
System.out.println("\nRemoving one element from Queue\n");
System.out.println("Queue is " + linkedQueue);
// print head of queue
System.out.println("Head of Queue is " + head);
}
}
队列是 [John,Tom,Clark,Kat]
队列的头是 John
从队列中删除一个元素
队列是 [Tom,Clark,Kat]
队列的头是 Tom
程序2:查找包含员工列表的 LinkedBlockingQueue 的头部。
// Java程序展示 peek() 方法使用
// LinkedBlockingQueue 类
import java.util.concurrent.LinkedBlockingQueue;
public class GFG {
public void findPeek()
{
// 定义 LinkedBlockingQueue 的容量
int capacityOfQueue = 7;
// 创建 LinkedBlockingQueue 对象
LinkedBlockingQueue<Employee> linkedQueue
= new LinkedBlockingQueue<Employee>(capacityOfQueue);
// 向 LinkedBlockingQueue 中添加元素
Employee emp1 = new Employee("Ravi", "Tester", "39000");
Employee emp2 = new Employee("Sanjeet", "Manager", "98000");
// 将 Employee 对象添加到 linkedQueue 中
linkedQueue.add(emp1);
linkedQueue.add(emp2);
// 打印首部元素,然后移除该元素
// 重复此过程,直到队列为空
while (linkedQueue.size() != 0) {
// 使用 peek() 方法找到 linkedQueue 的头部
Employee head = linkedQueue.peek();
// 打印结果
System.out.println("队列头部");
System.out.println("员工姓名:" + head.name);
System.out.println("员工职位:" + head.position);
System.out.println("员工薪资:" + head.salary);
linkedQueue.remove();
if (linkedQueue.size() != 0)
System.out.println("\n从队列中移除一个元素\n");
}
}
// 创建一个带有 position 和 salary 属性的 Employee 对象
public class Employee {
public String name;
public String position;
public String salary;
Employee(String name, String position, String salary)
{
this.name = name;
this.position = position;
this.salary = salary;
}
@Override
public String toString()
{
return "Employee [name=" + name + ", position="
+ position + ", salary=" + salary + "]";
}
}
// 主函数
public static void main(String[] args)
{
GFG gfg = new GFG();
gfg.findPeek();
}
}
队列头部
员工姓名:Ravi
员工职位:Tester
员工薪资:39000
从队列中移除一个元素
队列头部
员工姓名:Sanjeet
员工职位:Manager
员工薪资:98000
参考文献: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#peek–
极客教程