ConcurrentLinkedQueue的poll()方法用於刪除並返回此ConcurrentLinkedQueue的頭。如果ConcurrentLinkedQueue為空,則此方法將返回null。
用法:
public E poll()
返回值:此方法移除並返回此ConcurrentLinkedQueue的頭部;如果此隊列為空,則返回null。
以下示例程序旨在說明ConcurrentLinkedQueue的poll()方法:
示例1:
// Java Program Demonstrate poll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
// Add Numbers to queue
queue.add(4353);
queue.add(7824);
queue.add(78249);
queue.add(8724);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// apply poll()
int response1 = queue.poll();
// print after applying poll method
System.out.println("Head: " + response1);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Current ConcurrentLinkedQueue: " + queue);
}
}
輸出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724] Head: 4353 Current ConcurrentLinkedQueue: [7824, 78249, 8724]
示例2:
// Java Program Demonstrate poll()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
queue.add("Sanjeet");
queue.add("Rabi");
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// apply poll() on queue
String response1 = queue.poll();
// print after applying poll method
System.out.println("Head: " + response1);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Current ConcurrentLinkedQueue: " + queue);
// apply poll() on queue more than one time
queue.poll();
queue.poll();
// Displaying the existing ConcurrentLinkedQueue
System.out.println("After 2 poll() applied\n"
+ "ConcurrentLinkedQueue: " + queue);
}
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi] Head: Aman Current ConcurrentLinkedQueue: [Amar, Sanjeet, Rabi] After 2 poll() applied ConcurrentLinkedQueue: [Rabi]
參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#poll–
相關用法
- Java ConcurrentLinkedQueue add()用法及代碼示例
- Java ConcurrentLinkedQueue contains()用法及代碼示例
- Java ConcurrentLinkedQueue isEmpty()用法及代碼示例
- Java ConcurrentLinkedQueue iterator()用法及代碼示例
- Java ConcurrentLinkedQueue peek()用法及代碼示例
- Java ConcurrentLinkedQueue addAll()用法及代碼示例
- Java ConcurrentLinkedQueue offer()用法及代碼示例
- Java ConcurrentLinkedQueue size()用法及代碼示例
- Java ConcurrentLinkedQueue toArray()用法及代碼示例
- Java ConcurrentLinkedQueue spliterator()用法及代碼示例
- Java ConcurrentLinkedQueue remove()用法及代碼示例
- Java PriorityQueue poll()用法及代碼示例
- Java LinkedBlockingDeque poll()用法及代碼示例
- Java LinkedBlockingQueue poll()用法及代碼示例
- Java Queue poll()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ConcurrentLinkedQueue poll() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。