📜  Java中的队列 peek() 方法

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

Java中的队列 peek() 方法

队列接口peek()方法返回容器前面的元素。它不会删除容器中的元素。此方法返回队列的头部。当 Queue 为空时,该方法不会抛出异常,而是返回null

句法:

E peek()

Returns:该方法返回Queue的头部,当Queue为空时返回false

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

方案一:借助
链表

// 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 LinkedList();
  
        // 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:演示队列为空时队列的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 Q
            = new LinkedList();
  
        // 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 Q
            = new ArrayDeque();
  
        // 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 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

程序 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 Q
            = new ConcurrentLinkedDeque();
  
        // 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: Java/util/Queue.html#peek–