Friday, October 10, 2025
HomeLanguagesJavaDelayQueue remove() method in Java

DelayQueue remove() method in Java

The remove() method of DelayQueue class in Java is used to remove a single instance of the given object say obj from this DelayQueue if it is present. It returns true if the given element is removed successfully otherwise it returns false.

Syntax:

public boolean remove(Object obj)

Parameters: This method takes a single object obj as parameter which is to be removed from this DealyQueue.

Return Value: It returns a boolean value which is true if the element has been successfully deleted otherwise it returns false.

Below program illustrate the remove() method of DelayQueue in Java:




// Java Program to illustrate the remove method
// of DelayQueue class
  
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String args[])
    {
        // Create a DelayQueue instance
        DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
  
        // Create an object of type Delayed
        Delayed ob = new Delayed() {
            public long getDelay(TimeUnit unit)
            {
                return 24; // some value is returned
            }
  
            public int compareTo(Delayed o)
            {
                if (o.getDelay(TimeUnit.DAYS) > 
                                 this.getDelay(TimeUnit.DAYS))
                    return 1;
                else if (o.getDelay(TimeUnit.DAYS) == 
                                 this.getDelay(TimeUnit.DAYS))
                    return 0;
                return -1;
            }
        };
  
        // Add the object to DelayQueue
        queue.add(ob);
  
        // Print initial size of Queue
        System.out.println("Initial Size : "
                           + queue.size());
  
        // Remove the object ob from
        // this DelayQueue
        queue.remove(ob);
  
        // Print the final size of the DelayQueue
        System.out.println("Size after removing : "
                           + queue.size());
    }
}


Output:

Initial Size : 1
Size after removing : 0
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS