Open In App

PriorityBlockingQueue remove() method in Java

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The remove(Object o) method of PriorityBlockingQueue is used to delete an element from this queue. This method removes a single instance of the element passed as the parameter, if it is present.
It returns true if and only if the element was removed, else it returned false.

Syntax:

public boolean remove(Object o)

Parameter: This method accepts a mandatory parameter o which is the element to be removed from this queue, if present.

Return Value: This method returns true if the specified element was removed successfully. Else this method returns false.

Below programs illustrate remove() method in PriorityBlockingQueue:

Program 1:




// Java Program Demonstrate remove()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<Integer> pbq
            = new PriorityBlockingQueue<Integer>();
  
        // Add element to PriorityBlockingQueue
        pbq.put(1);
        pbq.put(2);
        pbq.put(3);
        pbq.put(4);
  
        // print queue
        System.out.println("Queue: " + pbq);
  
        // remove 2
        boolean res = pbq.remove(2);
        System.out.println("\n2 removed: " + res);
  
        // print queue
        System.out.println("Queue:  " + pbq);
  
        // remove 5
        res = pbq.remove(5);
        System.out.println("\n5 removed: " + res);
  
        // print queue
        System.out.println("Queue:  " + pbq);
    }
}


Output:

Queue: [1, 2, 3, 4]

2 removed: true
Queue:  [1, 4, 3]

5 removed: false
Queue:  [1, 4, 3]




// Java Program Demonstrate remove()
// method of PriorityBlockingQueue
  
import java.util.concurrent.PriorityBlockingQueue;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of PriorityBlockingQueue
        PriorityBlockingQueue<String> pbq
            = new PriorityBlockingQueue<String>();
  
        // Add element to PriorityBlockingQueue
        pbq.put("Geeks");
        pbq.put("forGeeks");
        pbq.put("A Computer");
        pbq.put("Portal");
  
        // print queue
        System.out.println("Queue: " + pbq);
  
        // remove Geeks
        boolean res = pbq.remove("Geeks");
        System.out.println("\nGeeks removed: " + res);
  
        // print queue
        System.out.println("Queue:  " + pbq);
  
        // remove SandeepJain
        res = pbq.remove("SandeepJain");
        System.out.println("\nSandeepJain removed: " + res);
  
        // print queue
        System.out.println("Queue:  " + pbq);
    }
}


Output:

Queue: [A Computer, Portal, Geeks, forGeeks]

Geeks removed: true
Queue:  [A Computer, Portal, forGeeks]

SandeepJain removed: false
Queue:  [A Computer, Portal, forGeeks]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads