當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 LinkedTransferQueue peek() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。