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:
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class GFG {
public static void main(String args[])
{
DelayQueue<Delayed> queue = new DelayQueue<Delayed>();
Delayed ob = new Delayed() {
public long getDelay(TimeUnit unit)
{
return 24 ;
}
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 ;
}
};
queue.add(ob);
System.out.println( "Initial Size : "
+ queue.size());
queue.remove(ob);
System.out.println( "Size after removing : "
+ queue.size());
}
}
|
Output:
Initial Size : 1
Size after removing : 0