📌  相关文章
📜  Java中的 PriorityBlockingQueue add() 方法

📅  最后修改于: 2022-05-13 01:54:40.697000             🧑  作者: Mango

Java中的 PriorityBlockingQueue add() 方法

PriorityBlockingQueueadd(E e)方法将作为参数传递的元素插入到此 PriorityBlockingQueue 尾部的方法中。如果添加元素成功,则此方法返回 true。否则返回false。

句法:

public boolean add(E e)

参数:该方法需要一个强制参数e ,它是要插入PriorityBlockingQueue的元素。

返回:此方法返回一个布尔响应。如果添加元素成功,则返回true ,否则返回false

异常:此方法抛出以下异常:

  • ClassCastException:如果作为参数传递的元素不能与队列包含的元素进行比较以保持优先队列的顺序。
  • NullPointerException:如果作为参数传递的元素为空。

下面的程序说明了 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 PrioBlockingQueue
            = new PriorityBlockingQueue(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 PrioBlockingQueue
            = new PriorityBlockingQueue(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 Java