PriorityBlockingQueue remove() method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Queue: [1, 2, 3, 4]

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

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

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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

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


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.