PriorityBlockingQueue的clear()方法從此隊列中刪除所有元素。因此,當需要清除PriorityBlockingQueue時,可以應用此方法。
用法:
public void clear()
參數:
此方法不帶參數。
返回值:
此方法不返回任何內容。
異常:
此方法不會引發任何異常。
以下示例程序旨在說明使用clear()方法從PriorityBlockingQueue中刪除所有元素。
示例1:
// Java Program to Demonstrate clear() method
// of PriorityBlockingQueue.
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<Integer> PrioBlockingQueue
= new PriorityBlockingQueue<Integer>(capacity);
// add numbers
PrioBlockingQueue.add(78758575);
PrioBlockingQueue.add(63447688);
PrioBlockingQueue.add(56434788);
// print queue after add operation
System.out.println("After Adding Numbers:");
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
// remove all the elements using clear() method
PrioBlockingQueue.clear();
// print queue after clear operation
System.out.println("\nAfter clear operation:");
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
}
}
輸出:
After Adding Numbers: PriorityBlockingQueue:[56434788, 78758575, 63447688] After clear operation: PriorityBlockingQueue:[]
示例2:為了說明在PriorityBlockingQueue上包含名稱列表的清晰方法。
// Java Program to Demonstrate clear() method
// of PriorityBlockingQueue.
import java.util.concurrent.PriorityBlockingQueue;
public class GFG {
public static void main(String[] args)
{
// define capacity of PriorityBlockingQueue
int capacity = 15;
// create object of PriorityBlockingQueue
PriorityBlockingQueue<String> PrioBlockingQueue
= new PriorityBlockingQueue<String>(capacity);
// add some names
PrioBlockingQueue.add("Tandrima");
PrioBlockingQueue.add("Argha");
PrioBlockingQueue.add("Arka");
// print queue after add operation
System.out.println("List of Names:");
System.out.println("PriorityBlockingQueue: "
+ PrioBlockingQueue);
// remove all the elements using clear() method
PrioBlockingQueue.clear();
// print queue after clear operation
System.out.println("\nAfter clearing List of names:");
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
}
}
輸出:
List of Names: PriorityBlockingQueue: [Argha, Tandrima, Arka] After clearing List of names: PriorityBlockingQueue:[]
參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#clear–
相關用法
- Java PriorityBlockingQueue take()用法及代碼示例
- Java PriorityBlockingQueue contains()用法及代碼示例
- Java PriorityBlockingQueue put()用法及代碼示例
- Java PriorityBlockingQueue add()用法及代碼示例
- Java PriorityBlockingQueue offer()用法及代碼示例
- Java PriorityBlockingQueue peek()用法及代碼示例
- Java PriorityBlockingQueue iterator()用法及代碼示例
- Java PriorityBlockingQueue poll()用法及代碼示例
- Java PriorityBlockingQueue spliterator()用法及代碼示例
- Java PriorityBlockingQueue toArray()用法及代碼示例
- Java PriorityBlockingQueue comparator()用法及代碼示例
- Java PriorityBlockingQueue drainTo()用法及代碼示例
- Java PriorityBlockingQueue toString()用法及代碼示例
- Java PriorityBlockingQueue remove()用法及代碼示例
- Java PriorityBlockingQueue remainingCapacity()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 PriorityBlockingQueue clear() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。