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


Java LinkedTransferQueue poll()用法及代码示例


java.util.concurrent.LinkedTransferQueue.poll()方法是Java中的内置函数,如果队列为非空,它将检索并删除队列的头部。

用法:

LinkedTransferQueue.poll()  

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


返回值:如果队列非空,则该函数返回队列的开头,否则返回null。

以下示例程序旨在说明LinkedTransferQueue.poll()方法:

示例1:

/* Java Program Demonstrate poll() 
   method of LinkedTransferQueue */
  
import java.util.concurrent.LinkedTransferQueue; 
  
class LinkedTransferQueuePollExample1 { 
    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.poll()); 
  
        // Printing remaining 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 :
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 poll() 
   method of LinkedTransferQueue */
  
import java.util.concurrent.LinkedTransferQueue; 
  
class LinkedTransferQueuePollExample2 { 
    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.poll()); 
  
        // Printing remaining 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 :
20 30 40 50

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



相关用法


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