Java 中的 Queue peek() 方法
Queue Interface 的 peek() 方法返回容器的前端的元素。 它不会删除容器中的元素。 该方法返回队列的头部。 当队列为空时,该方法不会引发异常,而是返回 null。
语法:
E peek()
返回值: 当队列为空时,该方法返回 null;否则返回队列的头部。
下面的程序说明了 Queue 的 peek() 方法:
程序 1: 使用
LinkedList 实现。
// Java Program Demonstrate peek()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedList<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// print queue
System.out.println("Queue: " + Q);
// print head
System.out.println("Queue's head: " + Q.peek());
// print queue
System.out.println("Queue: " + Q);
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
Queue: [7855642, 35658786, 5278367, 74381793]
程序 2: 演示 Queue 为空时的 peek() 方法。
// Java Program Demonstrate peek()
// method of Queue when Queue is empty
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedList<Integer>();
// print queue
System.out.println("Queue: " + Q);
// print head
System.out.println("Queue's head: " + Q.peek());
}
}
Queue: []
Queue's head: null
程序 3: 使用 ArrayDeque 实现。
// Java Program Demonstrate peek()
// method of Queue
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ArrayDeque<Integer>();
// Add numbers to end of Queue
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
// print queue
System.out.println("Queue: " + Q);
// print head
System.out.println("Queue's head: " + Q.peek());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
程序 4: 使用 LinkedBlockingDeque 实现(由于 HTML 格式有限制,下面的程序代码将以普通文本的形式呈现) // Java Program Demonstrate peek() // method of Queue import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Queue Queue Q = new LinkedBlockingDeque(); // Add numbers to end of Queue Q.add(7855642); Q.add(35658786); Q.add(5278367); Q.add(74381793); // print queue System.out.println(“Queue: ” + Q); // print head System.out.println(“Queue’s head: ” + Q.peek()); } } 输出如下: Queue: [7855642, 35658786, 5278367, 74381793] Queue’s head: 7855642
// Java程序演示Queue的peek()方法
//引入java.util与java.util.concurrent.LinkedBlockingDeque类库
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
//创建Queue队列对象
Queue<Integer> Q
= new LinkedBlockingDeque<Integer>();
//将数字添加到Queue队列末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
//打印队列
System.out.println("Queue: " + Q);
//打印头部
System.out.println("Queue's head: " + Q.peek());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
程序5: 使用 ConcurrentLinkedDeque 类库。
// Java程序演示Queue的peek()方法
//引入java.util与java.util.concurrent.ConcurrentLinkedDeque类库
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
//创建Queue队列对象
Queue<Integer> Q
= new ConcurrentLinkedDeque<Integer>();
//将数字添加到Queue队列末尾
Q.add(7855642);
Q.add(35658786);
Q.add(5278367);
Q.add(74381793);
//打印队列
System.out.println("Queue: " + Q);
//打印头部
System.out.println("Queue's head: " + Q.peek());
}
}
Queue: [7855642, 35658786, 5278367, 74381793]
Queue's head: 7855642
参考文献: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#peek–
极客教程