A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/priorityblockingqueue-remove-method-in-java/ below:

PriorityBlockingQueue remove() method in Java

PriorityBlockingQueue remove() method in Java

Last Updated : 11 Jul, 2025

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
// 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
// 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]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4