Saturday, September 27, 2025
HomeLanguagesJavaPriorityBlockingQueue add() Method in Java

PriorityBlockingQueue add() Method in Java

The add(E e) method of PriorityBlockingQueue inserts the element passed as a parameter to the method at the tail of this PriorityBlockingQueue. This method returns true if the adding of the element is successful. Else it returns false. 

Syntax:

public boolean add(E e)

Parameter: This method takes a mandatory parameter e which is the element to be inserted in PriorityBlockingQueue. 

Returns: This method returns a boolean response. It returns true if the adding of the element is successful, else it returns false

Exception: This method throws following exceptions:

  • ClassCastException: if the element passed as parameter cannot be compared to the element contained by queue to keep priority queue’s ordering.
  • NullPointerException: if the element passed as parameter is null.

Below program illustrate  add() method of PriorityBlockingQueue: 

Example 1: 

Java




// 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);
    }
}


Output:

After Adding Some Numbers
PriorityBlockingQueue:[526734, 84879456, 4586415]

After adding Some More Numbers
PriorityBlockingQueue:[156116, 526734, 4586415, 84879456, 61651191]

Example 2: To demonstrate NullPointerException thrown by add() method. 

Java




// 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);
        }
    }
}


Output:

Exception when adding null: java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/PriorityBlockingQueue.html#add-E-

RELATED ARTICLES

Most Popular

Dominic
32323 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6690 POSTS0 COMMENTS
Nicole Veronica
11857 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11913 POSTS0 COMMENTS
Shaida Kate Naidoo
6807 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6768 POSTS0 COMMENTS