Java Queue peek()方法

Java Queue peek()方法

Queue接口peek() 方法返回容器中最前面的元素。它不会删除容器中的元素。该方法返回队列的头部。当队列为空时,该方法不会抛出一个异常,而是返回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()方法

// 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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程