PriorityBlockingQueue的add(E e)方法將作為參數傳遞的元素插入到此PriorityBlockingQueue尾部的方法中。如果成功添加元素,則此方法返回true。否則返回false。
用法:
public boolean add(E e)
參數:此方法采用強製參數e,該參數是要插入PriorityBlockingQueue的元素。
返回值:此方法返回布爾響應。如果成功添加元素,則返回true,否則返回false。
異常:此方法引發以下異常:
- ClassCastException:如果不能將作為參數傳遞的元素與隊列包含的元素進行比較以保持優先級隊列的順序。
- NullPointerException :如果作為參數傳遞的元素為null。
以下示例程序旨在說明PriorityBlockingQueue的add()方法:
示例1:
// Java Program to Demonstrate add(E e) 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(526734);
PrioBlockingQueue.add(84879456);
PrioBlockingQueue.add(4586415);
// print queue after add operation
System.out.println("After Adding Some Numbers");
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
// add more numbers
PrioBlockingQueue.add(156116);
PrioBlockingQueue.add(61651191);
// print queue after add operation
System.out.println("\nAfter adding Some More Numbers");
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
}
}
輸出:
After Adding Some Numbers PriorityBlockingQueue:[526734, 84879456, 4586415] After adding Some More Numbers PriorityBlockingQueue:[156116, 526734, 4586415, 84879456, 61651191]
示例2:演示由add()方法引發的NullPointerException。
// Java Program to Demonstrate Exception
// thrown by add(E e) 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(526734);
PrioBlockingQueue.add(84879456);
PrioBlockingQueue.add(4586415);
try {
// try to add null to PrioBlockingQueue
PrioBlockingQueue.add(null);
// print PrioBlockingQueue after add operation
System.out.println("PriorityBlockingQueue:"
+ PrioBlockingQueue);
}
catch (Exception e) {
System.out.println("Exception when adding null: "
+ e);
}
}
}
輸出:
Exception when adding null: java.lang.NullPointerException
參考:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#add-E-
相關用法
- Java PriorityBlockingQueue contains()用法及代碼示例
- Java PriorityBlockingQueue take()用法及代碼示例
- Java PriorityBlockingQueue put()用法及代碼示例
- Java PriorityBlockingQueue toString()用法及代碼示例
- Java PriorityBlockingQueue comparator()用法及代碼示例
- Java PriorityBlockingQueue size()用法及代碼示例
- Java PriorityBlockingQueue remainingCapacity()用法及代碼示例
- Java PriorityBlockingQueue remove()用法及代碼示例
- Java PriorityBlockingQueue iterator()用法及代碼示例
- Java PriorityBlockingQueue peek()用法及代碼示例
- Java PriorityBlockingQueue spliterator()用法及代碼示例
- Java PriorityBlockingQueue offer()用法及代碼示例
- Java PriorityBlockingQueue toArray()用法及代碼示例
- Java PriorityBlockingQueue poll()用法及代碼示例
- Java PriorityBlockingQueue clear()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 PriorityBlockingQueue add() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。