Java LinkedTransferQueue peek()方法

Java LinkedTransferQueue peek()方法

java.util.concurrent.LinkedTransferQueue.peek() 方法是Java中的一个内置函数,用于在队列非空时返回队首。

语法

LinkedTransferQueue.peek()  

参数: 该函数不接受任何参数。

返回值: 如果队列不是空的,该函数返回队列的头部,否则返回null。

下面的程序说明了LinkedTransferQueue.peek()方法。

程序1 :

// Java Program Demonstrate peek()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueuePeekExample1 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Character> queue = 
                      new LinkedTransferQueue<Character>();
  
        // Adding elements to this queue
        for (char ch = 'A'; ch <= 'Z'; ch++) {
            queue.add(ch);
        }
  
        // Printing the head of the queue
        System.out.println("The head of the queue is " 
                                        + queue.peek());
  
        // Printing all the elements of the queue
        System.out.println("The elements in the queue :");
        for (Character i : queue)
            System.out.print(i + " ");
    }
}

输出:

The head of the queue is A
The elements in the queue :
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

示例2

// Java Program Demonstrate peek()
// method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue;
  
class LinkedTransferQueuePeekExample2 {
    public static void main(String[] args)
    {
        // Initializing the queue
        LinkedTransferQueue<Integer> queue = 
                 new LinkedTransferQueue<Integer>();
  
        // Adding elements to this queue
        for (int i = 10; i <= 50; i += 10)
            queue.add(i);
  
        // Printing the head of the queue
        System.out.println("The head of the queue is " 
                                           + queue.peek());
  
        // Printing all the elements of the queue
        System.out.println("The elements in the queue :");
        for (Integer i : queue)
            System.out.print(i + " ");
    }
}

输出:

The head of the queue is 10
The elements in the queue :
10 20 30 40 50

参考资料 : https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedTransferQueue.html#peek()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程