隊列接口的peek()方法返回位於容器前麵的元素。它不會刪除容器中的元素。此方法返回隊列的開頭。當Queue為空時,該方法不會引發異常,而是返回null。
用法:
E peek()
返回值:此方法返回隊列的頭部,當隊列為空時返回false
以下示例程序旨在說明隊列的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() Queue方法
// 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。
// Java Program Demonstrate peek()
// method of Queue
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new LinkedBlockingDeque<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
示例5:借助ConcurrentLinkedDeque。
// Java Program Demonstrate peek()
// method of Queue
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
// create object of Queue
Queue<Integer> Q
= new ConcurrentLinkedDeque<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
參考: https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html#peek–
相關用法
- Java PriorityBlockingQueue peek()用法及代碼示例
- Java ArrayDeque peek()用法及代碼示例
- Java ConcurrentLinkedDeque peek()用法及代碼示例
- Java Stack peek()用法及代碼示例
- Java PriorityQueue peek()用法及代碼示例
- Java LinkedBlockingQueue peek()用法及代碼示例
- Java ArrayBlockingQueue peek()用法及代碼示例
- Java LinkedTransferQueue peek()用法及代碼示例
- Java ConcurrentLinkedQueue peek()用法及代碼示例
- Java LinkedBlockingDeque peek()用法及代碼示例
- Java BlockingDeque peek()用法及代碼示例
- Java Queue add()用法及代碼示例
- Java Queue element()用法及代碼示例
- Java Queue poll()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Queue peek() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。