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


Java PriorityBlockingQueue add()用法及代码示例


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-



相关用法


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