当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Queue peek()用法及代码示例


队列接口的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–



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Queue peek() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。